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 java.io.Serializable;
024import java.util.Date;
025
026import org.nuxeo.ecm.core.api.DocumentModel;
027
028/**
029 * Represents input parameters of a Template. Inputs parameters have an {@link InputType}, a name an a value. Value can
030 * be a xpath pointing to a {@link DocumentModel} property.
031 * 
032 * @author Tiry (tdelprat@nuxeo.com)
033 */
034public class TemplateInput implements Serializable {
035
036    private static final long serialVersionUID = 1L;
037
038    protected String name;
039
040    protected String stringValue;
041
042    protected Boolean booleanValue;
043
044    protected Date dateValue;
045
046    protected InputType type = InputType.StringValue;
047
048    protected String source;
049
050    protected String desciption;
051
052    protected boolean readOnly;
053
054    protected boolean autoLoop = false;
055
056    public TemplateInput(String name) {
057        this.name = name;
058        this.stringValue = "";
059    }
060
061    public TemplateInput(String name, Object value) {
062        this.name = name;
063        if (value instanceof String) {
064            stringValue = (String) value;
065            type = InputType.StringValue;
066        } else if (value instanceof Date) {
067            dateValue = (Date) value;
068            type = InputType.DateValue;
069        } else if (value instanceof Boolean) {
070            booleanValue = (Boolean) value;
071            type = InputType.BooleanValue;
072        }
073    }
074
075    public TemplateInput getCopy(boolean readOnly) {
076        TemplateInput item = new TemplateInput(name);
077        item.booleanValue = booleanValue;
078        item.dateValue = dateValue;
079        item.source = source;
080        item.desciption = desciption;
081        item.stringValue = stringValue;
082        item.type = type;
083        item.readOnly = readOnly;
084        item.autoLoop = autoLoop;
085        return item;
086    }
087
088    public TemplateInput update(TemplateInput other) {
089        this.name = other.name;
090        this.type = other.type;
091        this.autoLoop = other.autoLoop;
092        this.booleanValue = other.booleanValue;
093        this.dateValue = other.dateValue;
094        this.desciption = other.desciption;
095        this.readOnly = other.readOnly;
096        this.source = other.source;
097        this.stringValue = other.stringValue;
098        return this;
099    }
100
101    public String getSource() {
102        return source;
103    }
104
105    public void setSource(String source) {
106        this.source = source;
107    }
108
109    public String getDesciption() {
110        return desciption;
111    }
112
113    public void setDesciption(String desciption) {
114        this.desciption = desciption;
115    }
116
117    @Override
118    public String toString() {
119        String str = name + " (" + type + ") : '";
120        if (InputType.StringValue.equals(type) && stringValue != null) {
121            str = str + stringValue;
122        } else if (InputType.DateValue.equals(type) && dateValue != null) {
123            str = str + dateValue.toString();
124        } else if (InputType.BooleanValue.equals(type) && booleanValue != null) {
125            str = str + booleanValue.toString();
126        } else {
127            str = str + source;
128        }
129        return str + "'";
130    }
131
132    public String getName() {
133        return name;
134    }
135
136    public void setName(String name) {
137        this.name = name;
138    }
139
140    public String getStringValue() {
141        return stringValue;
142    }
143
144    public void setStringValue(String stringValue) {
145        this.stringValue = stringValue;
146    }
147
148    public Boolean getBooleanValue() {
149        if (booleanValue == null) {
150            return new Boolean(false);
151        }
152        return booleanValue;
153    }
154
155    public void setBooleanValue(Boolean booleanValue) {
156        this.booleanValue = booleanValue;
157    }
158
159    public Date getDateValue() {
160        if (dateValue == null) {
161            return new Date();
162        }
163        return dateValue;
164    }
165
166    public void setDateValue(Date dateValue) {
167        this.dateValue = dateValue;
168    }
169
170    public InputType getType() {
171        return type;
172    }
173
174    public String getTypeAsString() {
175        if (type == null) {
176            return "";
177        }
178        return type.toString();
179    }
180
181    public void setType(InputType type) {
182        this.type = type;
183    }
184
185    public void setTypeAsString(String strType) {
186        this.type = InputType.getByValue(strType);
187    }
188
189    public boolean isSimpleValue() {
190        return !isSourceValue();
191    }
192
193    public boolean isSourceValue() {
194        return (InputType.PictureProperty == type || InputType.DocumentProperty == type || InputType.Content == type);
195    }
196
197    public boolean isReadOnly() {
198        return readOnly;
199    }
200
201    public void setReadOnly(boolean readOnly) {
202        this.readOnly = readOnly;
203    }
204
205    public boolean isSet() {
206        return source != null || dateValue != null || booleanValue != null
207                || (stringValue != null && !stringValue.isEmpty());
208    }
209
210    public boolean isAutoLoop() {
211        return autoLoop;
212    }
213
214    public void setAutoLoop(boolean autoLoop) {
215        this.autoLoop = autoLoop;
216    }
217
218}