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    @Override
128    public String getId() {
129        return id;
130    }
131
132    @Override
133    public String getTagConfigId() {
134        StringBuilder builder = new StringBuilder();
135        builder.append(definitionId).append(";");
136        builder.append(mode).append(";");
137
138        Integer intValue = Integer.valueOf(builder.toString().hashCode());
139        return intValue.toString();
140    }
141
142    @Override
143    public void setId(String id) {
144        this.id = id;
145    }
146
147    @Override
148    public String getName() {
149        return name;
150    }
151
152    @Override
153    public String getMode() {
154        return mode;
155    }
156
157    @Override
158    public String getTemplate() {
159        return template;
160    }
161
162    @Override
163    public LayoutRow[] getRows() {
164        return rows;
165    }
166
167    @Override
168    public int getColumns() {
169        return columns;
170    }
171
172    @Override
173    public Widget getWidget(String name) {
174        return widgetMap.get(name);
175    }
176
177    @Override
178    public Map<String, Widget> getWidgetMap() {
179        return Collections.unmodifiableMap(widgetMap);
180    }
181
182    @Override
183    public Map<String, Serializable> getProperties() {
184        if (properties == null) {
185            return Collections.emptyMap();
186        }
187        return Collections.unmodifiableMap(properties);
188    }
189
190    @Override
191    public Serializable getProperty(String name) {
192        if (properties != null) {
193            return properties.get(name);
194        }
195        return null;
196    }
197
198    @Override
199    public void setProperty(String name, Serializable value) {
200        if (properties == null) {
201            properties = new HashMap<>();
202        }
203        properties.put(name, value);
204    }
205
206    @Override
207    public String getValueName() {
208        return valueName;
209    }
210
211    @Override
212    public void setValueName(String valueName) {
213        this.valueName = valueName;
214        // set it on all widgets too
215        if (rows == null || rows.length == 0) {
216            return;
217        }
218        for (LayoutRow row : rows) {
219            Widget[] widgets = row.getWidgets();
220            if (widgets == null || widgets.length == 0) {
221                continue;
222            }
223            for (Widget widget : widgets) {
224                if (widget != null) {
225                    widget.setValueName(valueName);
226                }
227            }
228        }
229    }
230
231    /**
232     * @since 6.0
233     */
234    @Override
235    public String getType() {
236        return type;
237    }
238
239    /**
240     * @since 6.0
241     */
242    public void setType(String type) {
243        this.type = type;
244    }
245
246    /**
247     * @since 6.0
248     */
249    @Override
250    public String getTypeCategory() {
251        return typeCategory;
252    }
253
254    /**
255     * @since 6.0
256     */
257    public void setTypeCategory(String typeCategory) {
258        this.typeCategory = typeCategory;
259    }
260
261    @Override
262    public String toString() {
263        final StringBuilder sb = new StringBuilder();
264
265        sb.append("LayoutImpl");
266        sb.append(" {");
267        sb.append(" name=");
268        sb.append(name);
269        sb.append(", id=");
270        sb.append(id);
271        sb.append(", mode=");
272        sb.append(mode);
273        sb.append(", template=");
274        sb.append(template);
275        sb.append(", properties=");
276        sb.append(properties);
277        sb.append('}');
278
279        return sb.toString();
280    }
281
282    @Override
283    public boolean isDynamic() {
284        return dynamic;
285    }
286
287    public void setDynamic(boolean dynamic) {
288        this.dynamic = dynamic;
289    }
290
291    @Override
292    public String getDevTemplate() {
293        return devTemplate;
294    }
295
296    public void setDevTemplate(String devTemplate) {
297        this.devTemplate = devTemplate;
298    }
299
300    @Override
301    public LayoutDefinition getDefinition() {
302        return definition;
303    }
304
305    public void setDefinition(LayoutDefinition definition) {
306        this.definition = definition;
307    }
308
309}