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