001/*
002 * (C) Copyright 2006-2016 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 *     <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
018 */
019package org.nuxeo.ecm.platform.forms.layout.api.impl;
020
021import java.io.Serializable;
022import java.util.Collections;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.nuxeo.ecm.platform.forms.layout.api.Layout;
028import org.nuxeo.ecm.platform.forms.layout.api.LayoutDefinition;
029import org.nuxeo.ecm.platform.forms.layout.api.LayoutRow;
030import org.nuxeo.ecm.platform.forms.layout.api.Widget;
031
032/**
033 * Implementation for layouts.
034 *
035 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
036 */
037public class LayoutImpl implements Layout {
038
039    private static final long serialVersionUID = -8975637002024432963L;
040
041    protected String id;
042
043    protected String name;
044
045    protected String type;
046
047    protected String typeCategory;
048
049    protected String mode;
050
051    protected String template;
052
053    protected String devTemplate;
054
055    protected LayoutRow[] rows;
056
057    protected Map<String, Widget> widgetMap;
058
059    protected int columns = 0;
060
061    protected Map<String, Serializable> properties;
062
063    protected String definitionId;
064
065    protected String valueName;
066
067    protected boolean dynamic = false;
068
069    protected LayoutDefinition definition;
070
071    // needed by GWT serialization
072    protected LayoutImpl() {
073        super();
074    }
075
076    /**
077     * @since 5.5
078     */
079    public LayoutImpl(String name, String mode, String template, List<LayoutRow> rows, int columns,
080            Map<String, Serializable> properties, String definitionId) {
081        this.name = name;
082        this.mode = mode;
083        this.template = template;
084        this.rows = rows.toArray(new LayoutRow[0]);
085        this.columns = columns;
086        this.properties = properties;
087        this.widgetMap = new HashMap<>();
088        computeWidgetMap();
089        this.definitionId = definitionId;
090    }
091
092    /**
093     * @since 8.1
094     */
095    public LayoutImpl(String name, String mode, String template, Map<String, Widget> widgets,
096            Map<String, Serializable> properties, String definitionId) {
097        this.name = name;
098        this.mode = mode;
099        this.template = template;
100        this.rows = new LayoutRow[0];
101        this.columns = 0;
102        this.properties = properties;
103        this.widgetMap = new HashMap<>();
104        if (widgets != null) {
105            this.widgetMap.putAll(widgets);
106        }
107        this.definitionId = definitionId;
108    }
109
110    protected void computeWidgetMap() {
111        if (rows == null || rows.length == 0) {
112            return;
113        }
114        for (LayoutRow row : rows) {
115            Widget[] widgets = row.getWidgets();
116            if (widgets == null || widgets.length == 0) {
117                continue;
118            }
119            for (Widget widget : widgets) {
120                if (widget != null) {
121                    widgetMap.put(widget.getName(), widget);
122                }
123            }
124        }
125    }
126
127    public String getId() {
128        return id;
129    }
130
131    @Override
132    public String getTagConfigId() {
133        StringBuilder builder = new StringBuilder();
134        builder.append(definitionId).append(";");
135        builder.append(mode).append(";");
136
137        Integer intValue = new Integer(builder.toString().hashCode());
138        return intValue.toString();
139    }
140
141    public void setId(String id) {
142        this.id = id;
143    }
144
145    public String getName() {
146        return name;
147    }
148
149    public String getMode() {
150        return mode;
151    }
152
153    public String getTemplate() {
154        return template;
155    }
156
157    public LayoutRow[] getRows() {
158        return rows;
159    }
160
161    public int getColumns() {
162        return columns;
163    }
164
165    public Widget getWidget(String name) {
166        return widgetMap.get(name);
167    }
168
169    public Map<String, Widget> getWidgetMap() {
170        return Collections.unmodifiableMap(widgetMap);
171    }
172
173    public Map<String, Serializable> getProperties() {
174        if (properties == null) {
175            return Collections.emptyMap();
176        }
177        return Collections.unmodifiableMap(properties);
178    }
179
180    public Serializable getProperty(String name) {
181        if (properties != null) {
182            return properties.get(name);
183        }
184        return null;
185    }
186
187    public void setProperty(String name, Serializable value) {
188        if (properties == null) {
189            properties = new HashMap<>();
190        }
191        properties.put(name, value);
192    }
193
194    @Override
195    public String getValueName() {
196        return valueName;
197    }
198
199    @Override
200    public void setValueName(String valueName) {
201        this.valueName = valueName;
202        // set it on all widgets too
203        if (rows == null || rows.length == 0) {
204            return;
205        }
206        for (LayoutRow row : rows) {
207            Widget[] widgets = row.getWidgets();
208            if (widgets == null || widgets.length == 0) {
209                continue;
210            }
211            for (Widget widget : widgets) {
212                if (widget != null) {
213                    widget.setValueName(valueName);
214                }
215            }
216        }
217    }
218
219    /**
220     * @since 6.0
221     */
222    public String getType() {
223        return type;
224    }
225
226    /**
227     * @since 6.0
228     */
229    public void setType(String type) {
230        this.type = type;
231    }
232
233    /**
234     * @since 6.0
235     */
236    public String getTypeCategory() {
237        return typeCategory;
238    }
239
240    /**
241     * @since 6.0
242     */
243    public void setTypeCategory(String typeCategory) {
244        this.typeCategory = typeCategory;
245    }
246
247    @Override
248    public String toString() {
249        final StringBuilder buf = new StringBuilder();
250
251        buf.append("LayoutImpl");
252        buf.append(" {");
253        buf.append(" name=");
254        buf.append(name);
255        buf.append(", id=");
256        buf.append(id);
257        buf.append(", mode=");
258        buf.append(mode);
259        buf.append(", template=");
260        buf.append(template);
261        buf.append(", properties=");
262        buf.append(properties);
263        buf.append('}');
264
265        return buf.toString();
266    }
267
268    public boolean isDynamic() {
269        return dynamic;
270    }
271
272    public void setDynamic(boolean dynamic) {
273        this.dynamic = dynamic;
274    }
275
276    public String getDevTemplate() {
277        return devTemplate;
278    }
279
280    public void setDevTemplate(String devTemplate) {
281        this.devTemplate = devTemplate;
282    }
283
284    public LayoutDefinition getDefinition() {
285        return definition;
286    }
287
288    public void setDefinition(LayoutDefinition definition) {
289        this.definition = definition;
290    }
291
292}