001/*
002 * (C) Copyright 2006-20012 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 */
018
019package org.nuxeo.template.api;
020
021import java.io.Serializable;
022import java.util.Date;
023
024import org.nuxeo.ecm.core.api.DocumentModel;
025
026/**
027 * Represents input parameters of a Template. Inputs parameters have an {@link InputType}, a name an a value. Value can
028 * be a xpath pointing to a {@link DocumentModel} property.
029 * 
030 * @author Tiry (tdelprat@nuxeo.com)
031 */
032public class TemplateInput implements Serializable {
033
034    private static final long serialVersionUID = 1L;
035
036    protected String name;
037
038    protected String stringValue;
039
040    protected Boolean booleanValue;
041
042    protected Date dateValue;
043
044    protected InputType type = InputType.StringValue;
045
046    protected String source;
047
048    protected String desciption;
049
050    protected boolean readOnly;
051
052    protected boolean autoLoop = false;
053
054    public TemplateInput(String name) {
055        this.name = name;
056    }
057
058    public TemplateInput(String name, Object value) {
059        this.name = name;
060        if (value instanceof String) {
061            stringValue = (String) value;
062            type = InputType.StringValue;
063        } else if (value instanceof Date) {
064            dateValue = (Date) value;
065            type = InputType.DateValue;
066        } else if (value instanceof Boolean) {
067            booleanValue = (Boolean) value;
068            type = InputType.BooleanValue;
069        }
070    }
071
072    public TemplateInput getCopy(boolean readOnly) {
073        TemplateInput item = new TemplateInput(name);
074        item.booleanValue = booleanValue;
075        item.dateValue = dateValue;
076        item.source = source;
077        item.desciption = desciption;
078        item.stringValue = stringValue;
079        item.type = type;
080        item.readOnly = readOnly;
081        item.autoLoop = autoLoop;
082        return item;
083    }
084
085    public TemplateInput update(TemplateInput other) {
086        this.name = other.name;
087        this.type = other.type;
088        this.autoLoop = other.autoLoop;
089        this.booleanValue = other.booleanValue;
090        this.dateValue = other.dateValue;
091        this.desciption = other.desciption;
092        this.readOnly = other.readOnly;
093        this.source = other.source;
094        this.stringValue = other.stringValue;
095        return this;
096    }
097
098    public String getSource() {
099        return source;
100    }
101
102    public void setSource(String source) {
103        this.source = source;
104    }
105
106    public String getDesciption() {
107        return desciption;
108    }
109
110    public void setDesciption(String desciption) {
111        this.desciption = desciption;
112    }
113
114    @Override
115    public String toString() {
116        String str = name + " (" + type + ") : '";
117        if (InputType.StringValue.equals(type) && stringValue != null) {
118            str = str + stringValue;
119        } else if (InputType.DateValue.equals(type) && dateValue != null) {
120            str = str + dateValue.toString();
121        } else if (InputType.BooleanValue.equals(type) && booleanValue != null) {
122            str = str + booleanValue.toString();
123        } else {
124            str = str + source;
125        }
126        return str + "'";
127    }
128
129    public String getName() {
130        return name;
131    }
132
133    public void setName(String name) {
134        this.name = name;
135    }
136
137    public String getStringValue() {
138        return stringValue;
139    }
140
141    public void setStringValue(String stringValue) {
142        this.stringValue = stringValue;
143    }
144
145    public Boolean getBooleanValue() {
146        if (booleanValue == null) {
147            return new Boolean(false);
148        }
149        return booleanValue;
150    }
151
152    public void setBooleanValue(Boolean booleanValue) {
153        this.booleanValue = booleanValue;
154    }
155
156    public Date getDateValue() {
157        if (dateValue == null) {
158            return new Date();
159        }
160        return dateValue;
161    }
162
163    public void setDateValue(Date dateValue) {
164        this.dateValue = dateValue;
165    }
166
167    public InputType getType() {
168        return type;
169    }
170
171    public String getTypeAsString() {
172        if (type == null) {
173            return "";
174        }
175        return type.toString();
176    }
177
178    public void setType(InputType type) {
179        this.type = type;
180    }
181
182    public void setTypeAsString(String strType) {
183        this.type = InputType.getByValue(strType);
184    }
185
186    public boolean isSimpleValue() {
187        return !isSourceValue();
188    }
189
190    public boolean isSourceValue() {
191        return (InputType.PictureProperty == type || InputType.DocumentProperty == type || InputType.Content == type);
192    }
193
194    public boolean isReadOnly() {
195        return readOnly;
196    }
197
198    public void setReadOnly(boolean readOnly) {
199        this.readOnly = readOnly;
200    }
201
202    public boolean isSet() {
203        return source != null || dateValue != null || booleanValue != null
204                || (stringValue != null && !stringValue.isEmpty());
205    }
206
207    public boolean isAutoLoop() {
208        return autoLoop;
209    }
210
211    public void setAutoLoop(boolean autoLoop) {
212        this.autoLoop = autoLoop;
213    }
214
215}