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    }
059
060    public TemplateInput(String name, Object value) {
061        this.name = name;
062        if (value instanceof String) {
063            stringValue = (String) value;
064            type = InputType.StringValue;
065        } else if (value instanceof Date) {
066            dateValue = (Date) value;
067            type = InputType.DateValue;
068        } else if (value instanceof Boolean) {
069            booleanValue = (Boolean) value;
070            type = InputType.BooleanValue;
071        }
072    }
073
074    public TemplateInput getCopy(boolean readOnly) {
075        TemplateInput item = new TemplateInput(name);
076        item.booleanValue = booleanValue;
077        item.dateValue = dateValue;
078        item.source = source;
079        item.desciption = desciption;
080        item.stringValue = stringValue;
081        item.type = type;
082        item.readOnly = readOnly;
083        item.autoLoop = autoLoop;
084        return item;
085    }
086
087    public TemplateInput update(TemplateInput other) {
088        this.name = other.name;
089        this.type = other.type;
090        this.autoLoop = other.autoLoop;
091        this.booleanValue = other.booleanValue;
092        this.dateValue = other.dateValue;
093        this.desciption = other.desciption;
094        this.readOnly = other.readOnly;
095        this.source = other.source;
096        this.stringValue = other.stringValue;
097        return this;
098    }
099
100    public String getSource() {
101        return source;
102    }
103
104    public void setSource(String source) {
105        this.source = source;
106    }
107
108    public String getDesciption() {
109        return desciption;
110    }
111
112    public void setDesciption(String desciption) {
113        this.desciption = desciption;
114    }
115
116    @Override
117    public String toString() {
118        String str = name + " (" + type + ") : '";
119        if (InputType.StringValue.equals(type) && stringValue != null) {
120            str = str + stringValue;
121        } else if (InputType.DateValue.equals(type) && dateValue != null) {
122            str = str + dateValue.toString();
123        } else if (InputType.BooleanValue.equals(type) && booleanValue != null) {
124            str = str + booleanValue.toString();
125        } else {
126            str = str + source;
127        }
128        return str + "'";
129    }
130
131    public String getName() {
132        return name;
133    }
134
135    public void setName(String name) {
136        this.name = name;
137    }
138
139    public String getStringValue() {
140        return stringValue;
141    }
142
143    public void setStringValue(String stringValue) {
144        this.stringValue = stringValue;
145    }
146
147    public Boolean getBooleanValue() {
148        if (booleanValue == null) {
149            return new Boolean(false);
150        }
151        return booleanValue;
152    }
153
154    public void setBooleanValue(Boolean booleanValue) {
155        this.booleanValue = booleanValue;
156    }
157
158    public Date getDateValue() {
159        if (dateValue == null) {
160            return new Date();
161        }
162        return dateValue;
163    }
164
165    public void setDateValue(Date dateValue) {
166        this.dateValue = dateValue;
167    }
168
169    public InputType getType() {
170        return type;
171    }
172
173    public String getTypeAsString() {
174        if (type == null) {
175            return "";
176        }
177        return type.toString();
178    }
179
180    public void setType(InputType type) {
181        this.type = type;
182    }
183
184    public void setTypeAsString(String strType) {
185        this.type = InputType.getByValue(strType);
186    }
187
188    public boolean isSimpleValue() {
189        return !isSourceValue();
190    }
191
192    public boolean isSourceValue() {
193        return (InputType.PictureProperty == type || InputType.DocumentProperty == type || InputType.Content == type);
194    }
195
196    public boolean isReadOnly() {
197        return readOnly;
198    }
199
200    public void setReadOnly(boolean readOnly) {
201        this.readOnly = readOnly;
202    }
203
204    public boolean isSet() {
205        return source != null || dateValue != null || booleanValue != null
206                || (stringValue != null && !stringValue.isEmpty());
207    }
208
209    public boolean isAutoLoop() {
210        return autoLoop;
211    }
212
213    public void setAutoLoop(boolean autoLoop) {
214        this.autoLoop = autoLoop;
215    }
216
217}