1 package org.synchronoss.cpo.core.meta.domain;
2
3 /*-
4 * [[
5 * core
6 * ==
7 * Copyright (C) 2003 - 2026 Exaxis LLC, Synchronoss Technologies Inc
8 * ==
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Lesser Public License for more details.
18 *
19 * You should have received a copy of the GNU General Lesser Public
20 * License along with this program. If not, see
21 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22 * ]]
23 */
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.synchronoss.cpo.core.meta.bean.CpoFunctionBean;
30
31 /**
32 * Runtime metadata for a single CPO function: extends {@link CpoFunctionBean}'s name/expression/
33 * description with the ordered list of {@link CpoArgument}s bound to the function's positional bind
34 * markers.
35 *
36 * @author dberry
37 */
38 public class CpoFunction extends CpoFunctionBean {
39
40 private static final long serialVersionUID = 1L;
41
42 private static final Logger logger = LoggerFactory.getLogger(CpoFunction.class);
43
44 /** The arguments bound to this function's bind markers, in bind-marker order. */
45 List<CpoArgument> arguments = new ArrayList<>();
46
47 /** Creates an empty instance. */
48 public CpoFunction() {}
49
50 /**
51 * Gets the arguments bound to this function's bind markers, in bind-marker order.
52 *
53 * @return the bound arguments
54 */
55 public List<CpoArgument> getArguments() {
56 return arguments;
57 }
58
59 /**
60 * Appends an argument to this function's argument list. A no-op if {@code argument} is {@code
61 * null}.
62 *
63 * @param argument the argument to add
64 */
65 public void addArgument(CpoArgument argument) {
66 if (argument != null) {
67 arguments.add(argument);
68 }
69 }
70
71 /**
72 * Removes an argument from this function's argument list.
73 *
74 * @param argument the argument to remove
75 * @return {@code true} if the argument was found and removed, {@code false} otherwise
76 */
77 public boolean removeArgument(CpoArgument argument) {
78 if (argument != null) {
79 return arguments.remove(argument);
80 }
81 return false;
82 }
83
84 /**
85 * Removes the argument at the given position in this function's argument list.
86 *
87 * @param index the position of the argument to remove
88 * @return {@code true} if {@code index} was in range and the argument was removed, {@code false}
89 * otherwise
90 */
91 public boolean removeArgument(int index) {
92 if (index >= 0 && index < arguments.size()) {
93 return arguments.remove(index) != null;
94 }
95 return false;
96 }
97
98 /**
99 * Builds a diagnostic string describing {@code function}'s argument list: each argument's bound
100 * attribute Java type, for debugging/logging purposes.
101 *
102 * @param function the function whose arguments should be described, may be {@code null}
103 * @return a diagnostic description of the argument list, or {@code " null function."} if {@code
104 * function} is {@code null}
105 */
106 public String parameterToString(CpoFunction function) {
107 List<CpoArgument> args;
108 int j;
109 CpoArgument argument;
110 CpoAttribute attribute;
111 int type = 0;
112 Class<?> c;
113 StringBuilder sb = new StringBuilder("Parameter list for ");
114
115 if (function == null) {
116 return " null function.";
117 }
118
119 // TODO make uncomment the following line and make work
120 // sb.append(jq.getName() + " " + jq.getType());
121 args = function.getArguments();
122
123 for (j = 1; j <= args.size(); j++) {
124 argument = args.get(j - 1);
125
126 if (argument != null) {
127 try {
128 attribute = argument.getAttribute();
129 c = attribute.getGetter().getReturnType();
130 // TODO make uncomment the following line and make work
131 // type = attribute.getJavaSqlType();
132 if (c != null) {
133 sb.append(" col" + j + ":" + c.getName() + " type:" + type + " ");
134 } else {
135 sb.append(j + ":null type:" + type + " ");
136 }
137 } catch (Exception e) {
138 String msg = "parameterToString() Failed:";
139 logger.error(msg);
140 }
141 }
142 }
143
144 return sb.toString();
145 }
146
147 /**
148 * {@inheritDoc}
149 *
150 * <p>Returns this function's {@link #getName() name}.
151 */
152 @Override
153 public String toString() {
154 return this.getName();
155 }
156
157 /**
158 * Gets the full field-by-field string representation of this function, as produced by {@link
159 * CpoFunctionBean#toString()}.
160 *
161 * @return the full string representation
162 */
163 public String toStringFull() {
164 return super.toString();
165 }
166 }