001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 */
020
021package org.nuxeo.template.api;
022
023import static java.lang.Boolean.TRUE;
024import static org.nuxeo.template.api.InputType.BooleanValue;
025import static org.nuxeo.template.api.InputType.DateValue;
026import static org.nuxeo.template.api.InputType.ListValue;
027import static org.nuxeo.template.api.InputType.MapValue;
028import static org.nuxeo.template.api.InputType.StringValue;
029
030import java.io.Serializable;
031import java.util.ArrayList;
032import java.util.Date;
033import java.util.LinkedHashMap;
034import java.util.List;
035import java.util.Map;
036
037import org.nuxeo.ecm.core.api.DocumentModel;
038
039/**
040 * Represents input parameters of a Template. Inputs parameters have an {@link InputType}, a name an a value. Value can
041 * be a xpath pointing to a {@link DocumentModel} property.
042 *
043 * @author Tiry (tdelprat@nuxeo.com)
044 */
045public class TemplateInput implements Serializable {
046
047    private static final long serialVersionUID = 1L;
048
049    protected String name;
050
051    protected String stringValue;
052
053    protected Boolean booleanValue;
054
055    protected Date dateValue;
056
057    protected InputType type = StringValue;
058
059    protected String source;
060
061    protected String desciption;
062
063    protected boolean readOnly;
064
065    protected boolean autoLoop = false;
066
067    private Map<String, TemplateInput> mapValue;
068
069    public TemplateInput(String name) {
070        this.name = name;
071        this.stringValue = "";
072    }
073
074    public TemplateInput(String name, Object value) {
075        this.name = name;
076        if (value instanceof String) {
077            stringValue = (String) value;
078            type = StringValue;
079        } else if (value instanceof Date) {
080            dateValue = (Date) value;
081            type = InputType.DateValue;
082        } else if (value instanceof Boolean) {
083            booleanValue = (Boolean) value;
084            type = InputType.BooleanValue;
085        }
086    }
087
088    public TemplateInput getCopy(boolean readOnly) {
089        TemplateInput item = new TemplateInput(name);
090        item.booleanValue = booleanValue;
091        item.dateValue = dateValue;
092        item.source = source;
093        item.desciption = desciption;
094        item.stringValue = stringValue;
095        item.mapValue = mapValue;
096        item.type = type;
097        item.readOnly = readOnly;
098        item.autoLoop = autoLoop;
099        return item;
100    }
101
102    public TemplateInput update(TemplateInput other) {
103        this.name = other.name;
104        this.type = other.type;
105        this.autoLoop = other.autoLoop;
106        this.booleanValue = other.booleanValue;
107        this.dateValue = other.dateValue;
108        this.mapValue = other.mapValue;
109        this.desciption = other.desciption;
110        this.readOnly = other.readOnly;
111        this.source = other.source;
112        this.stringValue = other.stringValue;
113        return this;
114    }
115
116    public String getSource() {
117        return source;
118    }
119
120    public void setSource(String source) {
121        this.source = source;
122    }
123
124    public String getDesciption() {
125        return desciption;
126    }
127
128    public void setDesciption(String desciption) {
129        this.desciption = desciption;
130    }
131
132    @Override
133    public String toString() {
134        String str = name + " (" + type + ") : '";
135        if (StringValue.equals(type) && stringValue != null) {
136            str = str + stringValue;
137        } else if (DateValue.equals(type) && dateValue != null) {
138            str = str + dateValue.toString();
139        } else if (BooleanValue.equals(type) && booleanValue != null) {
140            str = str + booleanValue.toString();
141        } else if ((MapValue.equals(type) || (ListValue.equals(type))) && mapValue != null) {
142            str = str + mapValue.toString();
143        } else {
144            str = str + source;
145        }
146        return str + "'";
147    }
148
149    public String getName() {
150        return name;
151    }
152
153    public void setName(String name) {
154        this.name = name;
155    }
156
157    public String getStringValue() {
158        return stringValue;
159    }
160
161    public void setStringValue(Object stringValue) {
162        this.stringValue = (String) stringValue;
163    }
164
165    public Boolean getBooleanValue() {
166        if (booleanValue == null) {
167            return Boolean.FALSE;
168        }
169        return booleanValue;
170    }
171
172    public void setBooleanValue(Object booleanValue) {
173        if (booleanValue == null) {
174            this.booleanValue = Boolean.FALSE;
175        } else if (booleanValue instanceof String) {
176            this.booleanValue = Boolean.valueOf((String) booleanValue);
177        } else {
178            this.booleanValue = (Boolean) booleanValue;
179        }
180    }
181
182    public void setBooleanValue(boolean booleanValue) {
183        this.booleanValue = booleanValue;
184    }
185
186    public Date getDateValue() {
187        if (dateValue == null) {
188            return new Date();
189        }
190        return dateValue;
191    }
192
193    public void setDateValue(Object dateValue) {
194        this.dateValue = (Date) dateValue;
195    }
196
197    public void setMapValue(Object value) {
198        if (value instanceof List) {
199            this.mapValue = new LinkedHashMap<>();
200            for (TemplateInput child : (List<TemplateInput>) value) {
201                mapValue.put(child.getName(), child);
202            }
203        } else if (value instanceof Map) {
204            this.mapValue = (Map<String, TemplateInput>) value;
205        }
206    }
207
208    public Map<String, TemplateInput> getMapValue() {
209        return mapValue;
210    }
211
212    public List<TemplateInput> getListValue() {
213        return new ArrayList<>(mapValue.values());
214    }
215
216    public InputType getType() {
217        return type;
218    }
219
220    public String getTypeAsString() {
221        if (type == null) {
222            return "";
223        }
224        return type.toString();
225    }
226
227    public void setType(InputType type) {
228        this.type = type;
229    }
230
231    public void setTypeAsString(String strType) {
232        this.type = InputType.getByValue(strType);
233    }
234
235    public boolean isSimpleValue() {
236        return !isSourceValue();
237    }
238
239    public boolean isSourceValue() {
240        return (InputType.PictureProperty == type || InputType.DocumentProperty == type || InputType.Content == type);
241    }
242
243    public boolean isReadOnly() {
244        return readOnly;
245    }
246
247    public void setReadOnly(boolean readOnly) {
248        this.readOnly = readOnly;
249    }
250
251    public boolean isSet() {
252        return source != null || dateValue != null || booleanValue != null
253                || (stringValue != null && !stringValue.isEmpty());
254    }
255
256    public boolean isAutoLoop() {
257        return autoLoop;
258    }
259
260    public void setAutoLoop(boolean autoLoop) {
261        this.autoLoop = autoLoop;
262    }
263
264    public static TemplateInput factory(String name, InputType type, Object value) {
265        return factory(name, type, value, null, false, false);
266    }
267
268    public static TemplateInput factory(String name, InputType type, Object value, String description,
269            Boolean isReadonly, Boolean isAutoloop) {
270        TemplateInput param = new TemplateInput(name);
271        param.setType(type);
272        param.setReadOnly(TRUE.equals(isReadonly));
273        param.setAutoLoop(TRUE.equals(isAutoloop));
274        param.setDesciption(description);
275        switch (type) {
276        case StringValue:
277            param.setStringValue(value);
278            break;
279        case BooleanValue:
280            param.setBooleanValue(value);
281            break;
282        case DateValue:
283            param.setDateValue(value);
284            break;
285        case MapValue:
286        case ListValue:
287            param.setMapValue(value);
288            break;
289        case DocumentProperty:
290        case PictureProperty:
291        case Content:
292            param.setSource((String) value);
293        }
294        return param;
295    }
296
297}