001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ui.components;
029
030import static org.opencms.workplace.Messages.GUI_LABEL_PROJECT_0;
031import static org.opencms.workplace.explorer.Messages.GUI_INPUT_CACHE_0;
032import static org.opencms.workplace.explorer.Messages.GUI_INPUT_CATEGORIES_0;
033import static org.opencms.workplace.explorer.Messages.GUI_INPUT_COPYRIGHT_0;
034import static org.opencms.workplace.explorer.Messages.GUI_INPUT_DATECREATED_0;
035import static org.opencms.workplace.explorer.Messages.GUI_INPUT_DATEEXPIRED_0;
036import static org.opencms.workplace.explorer.Messages.GUI_INPUT_DATELASTMODIFIED_0;
037import static org.opencms.workplace.explorer.Messages.GUI_INPUT_DATERELEASED_0;
038import static org.opencms.workplace.explorer.Messages.GUI_INPUT_INTERNAL_TYPE_0;
039import static org.opencms.workplace.explorer.Messages.GUI_INPUT_LOCKEDBY_0;
040import static org.opencms.workplace.explorer.Messages.GUI_INPUT_NAME_0;
041import static org.opencms.workplace.explorer.Messages.GUI_INPUT_NAVTEXT_0;
042import static org.opencms.workplace.explorer.Messages.GUI_INPUT_PATH_0;
043import static org.opencms.workplace.explorer.Messages.GUI_INPUT_PERMISSIONS_0;
044import static org.opencms.workplace.explorer.Messages.GUI_INPUT_SIZE_0;
045import static org.opencms.workplace.explorer.Messages.GUI_INPUT_STATE_0;
046import static org.opencms.workplace.explorer.Messages.GUI_INPUT_TITLE_0;
047import static org.opencms.workplace.explorer.Messages.GUI_INPUT_TYPE_0;
048import static org.opencms.workplace.explorer.Messages.GUI_INPUT_USERCREATED_0;
049import static org.opencms.workplace.explorer.Messages.GUI_INPUT_USERLASTMODIFIED_0;
050
051import org.opencms.db.CmsResourceState;
052import org.opencms.file.CmsPropertyDefinition;
053import org.opencms.ui.CmsVaadinUtils;
054import org.opencms.util.CmsStringUtil;
055import org.opencms.workplace.CmsWorkplace;
056
057import java.io.Serializable;
058import java.util.Arrays;
059import java.util.Collections;
060import java.util.List;
061import java.util.Locale;
062import java.util.Map;
063
064import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
065
066import com.google.common.collect.Maps;
067import com.vaadin.ui.Component;
068import com.vaadin.v7.data.util.converter.Converter;
069
070/**
071 * Class whose instances contain the static data needed for a table column.<p>
072 */
073public class CmsResourceTableProperty implements Serializable {
074
075    /**
076     * Converter for dates represented by their time stamp.<p>
077     */
078    public static class DateConverter implements Converter<String, Long> {
079
080        /** The serial version id. */
081        private static final long serialVersionUID = -54133335743460680L;
082
083        /**
084         * @see com.vaadin.v7.data.util.converter.Converter#convertToModel(java.lang.Object, java.lang.Class, java.util.Locale)
085         */
086        public Long convertToModel(String value, Class<? extends Long> targetType, Locale locale)
087        throws com.vaadin.v7.data.util.converter.Converter.ConversionException {
088
089            throw new UnsupportedOperationException();
090        }
091
092        /**
093         * @see com.vaadin.v7.data.util.converter.Converter#convertToPresentation(java.lang.Object, java.lang.Class, java.util.Locale)
094         */
095        public String convertToPresentation(Long value, Class<? extends String> targetType, Locale locale)
096        throws com.vaadin.v7.data.util.converter.Converter.ConversionException {
097
098            return value != null
099            ? CmsVaadinUtils.getWpMessagesForCurrentLocale().getDateTime(value.longValue())
100            : CmsWorkplace.DEFAULT_DATE_STRING;
101        }
102
103        /**
104         * @see com.vaadin.v7.data.util.converter.Converter#getModelType()
105         */
106        public Class<Long> getModelType() {
107
108            return Long.class;
109        }
110
111        /**
112         * @see com.vaadin.v7.data.util.converter.Converter#getPresentationType()
113         */
114        public Class<String> getPresentationType() {
115
116            return String.class;
117        }
118    }
119
120    /** Resource table property. */
121    public static final CmsResourceTableProperty PROPERTY_CACHE = new CmsResourceTableProperty(
122        "PROPERTY_CACHE",
123        CmsPropertyDefinition.PROPERTY_CACHE,
124        String.class,
125        null,
126        GUI_INPUT_CACHE_0,
127        true,
128        2,
129        0);
130
131    /** Resource table property. */
132    public static final CmsResourceTableProperty PROPERTY_COPYRIGHT = new CmsResourceTableProperty(
133        "PROPERTY_COPYRIGHT",
134        CmsPropertyDefinition.PROPERTY_COPYRIGHT,
135        String.class,
136        null,
137        GUI_INPUT_COPYRIGHT_0,
138        true,
139        2,
140        0);
141
142    /** Resource table property. */
143    public static final CmsResourceTableProperty PROPERTY_DATE_CREATED = new CmsResourceTableProperty(
144        "PROPERTY_DATE_CREATED",
145        Long.class,
146        null,
147        GUI_INPUT_DATECREATED_0,
148        true,
149        0,
150        150,
151        new DateConverter());
152
153    /** Resource table property. */
154    public static final CmsResourceTableProperty PROPERTY_DATE_EXPIRED = new CmsResourceTableProperty(
155        "PROPERTY_DATE_EXPIRED",
156        Long.class,
157        null,
158        GUI_INPUT_DATEEXPIRED_0,
159        true,
160        0,
161        150,
162        new DateConverter());
163
164    /** Resource table property. */
165    public static final CmsResourceTableProperty PROPERTY_DATE_MODIFIED = new CmsResourceTableProperty(
166        "PROPERTY_DATE_MODIFIED",
167        Long.class,
168        null,
169        GUI_INPUT_DATELASTMODIFIED_0,
170        true,
171        0,
172        150,
173        new DateConverter());
174
175    /** Resource table property. */
176    public static final CmsResourceTableProperty PROPERTY_DATE_RELEASED = new CmsResourceTableProperty(
177        "PROPERTY_DATE_RELEASED",
178        Long.class,
179        null,
180        GUI_INPUT_DATERELEASED_0,
181        true,
182        0,
183        150,
184        new DateConverter());
185
186    /** Resource table property. */
187    public static final CmsResourceTableProperty PROPERTY_DISABLED = new CmsResourceTableProperty(
188        "PROPERTY_DISABLED",
189        Boolean.class,
190        Boolean.FALSE,
191        "",
192        true,
193        0,
194        0);
195
196    /** Resource table property. */
197    public static final CmsResourceTableProperty PROPERTY_INSIDE_PROJECT = new CmsResourceTableProperty(
198        "PROPERTY_INSIDE_PROJECT",
199        Boolean.class,
200        Boolean.TRUE,
201        null,
202        true,
203        0,
204        0);
205
206    /** Resource table property. */
207    public static final CmsResourceTableProperty PROPERTY_IS_FOLDER = new CmsResourceTableProperty(
208        "PROPERTY_IS_FOLDER",
209        Boolean.class,
210        null,
211        null,
212        true,
213        0,
214        0);
215
216    /** Resource table property. */
217    public static final CmsResourceTableProperty PROPERTY_NAVIGATION_POSITION = new CmsResourceTableProperty(
218        "PROPERTY_NAVIGATION_POSITION",
219        Float.class,
220        null,
221        null,
222        true,
223        0,
224        0);
225
226    /** Resource table property. */
227    public static final CmsResourceTableProperty PROPERTY_NAVIGATION_TEXT = new CmsResourceTableProperty(
228        "PROPERTY_NAVIGATION_TEXT",
229        CmsPropertyDefinition.PROPERTY_NAVTEXT,
230        String.class,
231        null,
232        GUI_INPUT_NAVTEXT_0,
233        true,
234        2,
235        0);
236
237    /** Resource table property. */
238    public static final CmsResourceTableProperty PROPERTY_IN_NAVIGATION = new CmsResourceTableProperty(
239        "PROPERTY_IN_NAVIGATION",
240        Boolean.class,
241        Boolean.FALSE,
242        null,
243        true,
244        0,
245        0);
246
247    /** Resource table property. */
248    public static final CmsResourceTableProperty PROPERTY_PERMISSIONS = new CmsResourceTableProperty(
249        "PROPERTY_PERMISSIONS",
250        String.class,
251        null,
252        GUI_INPUT_PERMISSIONS_0,
253        true,
254        0,
255        100);
256
257    /** Resource table property. */
258    public static final CmsResourceTableProperty PROPERTY_PROJECT = new CmsResourceTableProperty(
259        "PROPERTY_PROJECT",
260        Component.class,
261        null,
262        GUI_LABEL_PROJECT_0,
263        true,
264        0,
265        40);
266
267    /** Resource table property. */
268    public static final CmsResourceTableProperty PROPERTY_RELEASED_NOT_EXPIRED = new CmsResourceTableProperty(
269        "PROPERTY_RELEASED_NOT_EXPIRED",
270        Boolean.class,
271        Boolean.TRUE,
272        null,
273        true,
274        0,
275        0);
276
277    /** Resource table property. */
278    public static final CmsResourceTableProperty PROPERTY_RESOURCE_NAME = new CmsResourceTableProperty(
279        "PROPERTY_RESOURCE_NAME",
280        String.class,
281        null,
282        GUI_INPUT_NAME_0,
283        false,
284        2,
285        0);
286
287    /** Resource table property. */
288    public static final CmsResourceTableProperty PROPERTY_RESOURCE_TYPE = new CmsResourceTableProperty(
289        "PROPERTY_RESOURCE_TYPE",
290        String.class,
291        null,
292        GUI_INPUT_TYPE_0,
293        true,
294        0,
295        180);
296
297    /** Resource table property. */
298    public static final CmsResourceTableProperty PROPERTY_INTERNAL_RESOURCE_TYPE = new CmsResourceTableProperty(
299        "PROPERTY_INTERNAL_RESOURCE_TYPE",
300        String.class,
301        null,
302        GUI_INPUT_INTERNAL_TYPE_0,
303        true,
304        0,
305        130);
306
307    /** Resource table property. */
308    public static final CmsResourceTableProperty PROPERTY_SITE_PATH = new CmsResourceTableProperty(
309        "PROPERTY_SITE_PATH",
310        String.class,
311        null,
312        GUI_INPUT_PATH_0,
313        true,
314        0,
315        0);
316
317    /** Resource table property. */
318    public static final CmsResourceTableProperty PROPERTY_SIZE = new CmsResourceTableProperty(
319        "PROPERTY_SIZE",
320        Integer.class,
321        null,
322        GUI_INPUT_SIZE_0,
323        true,
324        0,
325        100);
326
327    /** Resource table property. */
328    public static final CmsResourceTableProperty PROPERTY_STATE = new CmsResourceTableProperty(
329        "PROPERTY_STATE",
330        CmsResourceState.class,
331        null,
332        null,
333        true,
334        0,
335        0);
336
337    /** Resource table property. */
338    public static final CmsResourceTableProperty PROPERTY_STATE_NAME = new CmsResourceTableProperty(
339        "PROPERTY_STATE_NAME",
340        String.class,
341        null,
342        GUI_INPUT_STATE_0,
343        true,
344        0,
345        100);
346
347    /** Resource table property. */
348    public static final CmsResourceTableProperty PROPERTY_TITLE = new CmsResourceTableProperty(
349        "PROPERTY_TITLE",
350        CmsPropertyDefinition.PROPERTY_TITLE,
351        String.class,
352        null,
353        GUI_INPUT_TITLE_0,
354        true,
355        3,
356        0);
357
358    public static final CmsResourceTableProperty PROPERTY_CATEGORIES = new CmsResourceTableProperty(
359        "PROPERTY_CATEGORIES",
360        CmsResourceTable.CategoryLabel.class,
361        null,
362        GUI_INPUT_CATEGORIES_0,
363        true,
364        3,
365        0);
366
367    /** Resource table property. */
368    public static final CmsResourceTableProperty PROPERTY_TREE_CAPTION = new CmsResourceTableProperty(
369        "PROPERTY_TREE_CAPTION",
370        String.class,
371        null,
372        "",
373        false,
374        0,
375        40);
376
377    /** Resoure table property. */
378    public static final CmsResourceTableProperty PROPERTY_TYPE_ICON = new CmsResourceTableProperty(
379        "PROPERTY_TYPE_ICON",
380        Component.class,
381        null,
382        "",
383        false,
384        0,
385        40);
386
387    /** Resource table property. */
388    public static final CmsResourceTableProperty PROPERTY_USER_CREATED = new CmsResourceTableProperty(
389        "PROPERTY_USER_CREATED",
390        String.class,
391        null,
392        GUI_INPUT_USERCREATED_0,
393        true,
394        0,
395        150);
396
397    /** Resource table property. */
398    public static final CmsResourceTableProperty PROPERTY_USER_LOCKED = new CmsResourceTableProperty(
399        "PROPERTY_USER_LOCKED",
400        String.class,
401        null,
402        GUI_INPUT_LOCKEDBY_0,
403        true,
404        0,
405        150);
406
407    /** Resource table property. */
408    public static final CmsResourceTableProperty PROPERTY_USER_MODIFIED = new CmsResourceTableProperty(
409        "PROPERTY_USER_MODIFIED",
410        String.class,
411        null,
412        GUI_INPUT_USERLASTMODIFIED_0,
413        true,
414        0,
415        150);
416
417    /** Map to keep track of default columns by name. */
418    private static Map<String, CmsResourceTableProperty> m_columnsByName;
419
420    /** The serial version id. */
421    private static final long serialVersionUID = -8006568789417647500L;
422
423    /** The column collapsible flag. */
424    private boolean m_collapsible;
425
426    /** The column type. */
427    private Class<?> m_columnType;
428
429    /** The column width. */
430    private int m_columnWidth;
431
432    /** The property to presentation string converter. */
433    private Converter<String, ?> m_converter;
434
435    /** Default value for the column. */
436    private Object m_defaultValue;
437
438    /** The editable property id. */
439    private String m_editPropertyId;
440
441    /** The column expand ratio. */
442    private float m_expandRatio;
443
444    /** The message key for the column header. */
445    private String m_headerKey;
446
447    /** The column id. */
448    private String m_id;
449
450    /**
451     * Creates a new instance.<p>
452     *
453     * @param id the id (should be unique)
454     * @param columnType the column type
455     * @param defaultValue the default value
456     * @param headerKey the message key for the header
457     * @param collapsible the column collapsible flag
458     * @param expandRation the column expand ratio
459     * @param columnWidth the column width
460     */
461    public CmsResourceTableProperty(
462        String id,
463        Class<?> columnType,
464        Object defaultValue,
465        String headerKey,
466        boolean collapsible,
467        float expandRation,
468        int columnWidth) {
469
470        this(id, columnType, defaultValue, headerKey, collapsible, expandRation, columnWidth, null);
471    }
472
473    /**
474     * Creates a new instance.<p>
475     *
476     * @param id the id (should be unique)
477     * @param columnType the column type
478     * @param defaultValue the default value
479     * @param headerKey the message key for the header
480     * @param collapsible the column collapsible flag
481     * @param expandRation the column expand ratio
482     * @param columnWidth the column width
483     * @param converter the property converter
484     */
485    public CmsResourceTableProperty(
486        String id,
487        Class<?> columnType,
488        Object defaultValue,
489        String headerKey,
490        boolean collapsible,
491        float expandRation,
492        int columnWidth,
493        Converter<String, ?> converter) {
494
495        m_id = id;
496        m_columnType = columnType;
497        m_defaultValue = defaultValue;
498        m_headerKey = headerKey;
499        m_collapsible = collapsible;
500        m_expandRatio = expandRation;
501        m_columnWidth = columnWidth;
502        m_converter = converter;
503    }
504
505    /**
506     * Creates a new instance.<p>
507     *
508     * @param id the id (should be unique)
509     * @param editPropertyId the editable property id
510     * @param columnType the column type
511     * @param defaultValue the default value
512     * @param headerKey the message key for the header
513     * @param collapsible the column collapsible flag
514     * @param expandRation the column expand ratio
515     * @param columnWidth the column width
516     */
517    public CmsResourceTableProperty(
518        String id,
519        String editPropertyId,
520        Class<?> columnType,
521        Object defaultValue,
522        String headerKey,
523        boolean collapsible,
524        float expandRation,
525        int columnWidth) {
526
527        this(id, columnType, defaultValue, headerKey, collapsible, expandRation, columnWidth, null);
528        m_editPropertyId = editPropertyId;
529    }
530
531    /**
532     * Gets the list of default columns.<p>
533     *
534     * @return the default columns
535     */
536    public static List<CmsResourceTableProperty> defaultProperties() {
537
538        return Arrays.asList(
539            PROPERTY_PROJECT,
540            PROPERTY_DATE_CREATED,
541            PROPERTY_DATE_EXPIRED,
542            PROPERTY_DATE_MODIFIED,
543            PROPERTY_DATE_RELEASED,
544            PROPERTY_IS_FOLDER,
545            PROPERTY_NAVIGATION_TEXT,
546            PROPERTY_COPYRIGHT,
547            PROPERTY_CACHE,
548            PROPERTY_PERMISSIONS,
549            PROPERTY_RESOURCE_NAME,
550            PROPERTY_RESOURCE_TYPE,
551            PROPERTY_INTERNAL_RESOURCE_TYPE,
552            PROPERTY_SIZE,
553            PROPERTY_STATE,
554            PROPERTY_STATE_NAME,
555            PROPERTY_TITLE,
556            PROPERTY_TYPE_ICON,
557            PROPERTY_USER_CREATED,
558            PROPERTY_USER_LOCKED,
559            PROPERTY_USER_MODIFIED,
560            PROPERTY_CATEGORIES);
561    }
562
563    /**
564     * Gets a map of default columns by name.<p>
565     *
566     * @return the default columns with their names as keys
567     */
568    public static Map<String, CmsResourceTableProperty> getDefaultColumnsByName() {
569
570        if (m_columnsByName == null) {
571            m_columnsByName = Maps.newHashMap();
572            for (CmsResourceTableProperty column : defaultProperties()) {
573                m_columnsByName.put(column.getId(), column);
574            }
575
576        }
577        return Collections.unmodifiableMap(m_columnsByName);
578    }
579
580    /**
581     * @see java.lang.Object#equals(java.lang.Object)
582     */
583    @Override
584    public boolean equals(Object other) {
585
586        return (other instanceof CmsResourceTableProperty) && ((CmsResourceTableProperty)other).m_id.equals(m_id);
587    }
588
589    /**
590     * Returns the columnType.<p>
591     *
592     * @return the columnType
593     */
594    public Class<?> getColumnType() {
595
596        return m_columnType;
597    }
598
599    /**
600     * Returns the column width.<p>
601     *
602     * @return the column width
603     */
604    public int getColumnWidth() {
605
606        return m_columnWidth;
607    }
608
609    /**
610     * Returns the property converter.<p>
611     *
612     * @return the converter
613     */
614    public Converter<String, ?> getConverter() {
615
616        return m_converter;
617    }
618
619    /**
620     * Returns the defaultValue.<p>
621     *
622     * @return the defaultValue
623     */
624    public Object getDefaultValue() {
625
626        return m_defaultValue;
627    }
628
629    /**
630     * Returns the edit property id.<p>
631     *
632     * @return the edit property id
633     */
634    public String getEditPropertyId() {
635
636        return m_editPropertyId;
637    }
638
639    /**
640     * Returns the expand ratio.<p>
641     *
642     * @return the expand ratio
643     */
644    public float getExpandRatio() {
645
646        return m_expandRatio;
647    }
648
649    /**
650     * Returns the headerKey.<p>
651     *
652     * @return the headerKey
653     */
654    public String getHeaderKey() {
655
656        return m_headerKey;
657    }
658
659    /**
660     * Gets the id of the column.<p>
661     *
662     * @return the id
663     */
664    public String getId() {
665
666        return m_id;
667    }
668
669    /**
670     * @see java.lang.Object#hashCode()
671     */
672    @Override
673    public int hashCode() {
674
675        return m_id.hashCode();
676    }
677
678    /**
679     * Returns the column collapsible flag.<p>
680     *
681     * @return the column collapsible flag
682     */
683    public boolean isCollapsible() {
684
685        return m_collapsible;
686    }
687
688    /**
689     * Checks whether this is an edit property.<p>
690     *
691     * @return <code>true</code> if this is an edit property
692     */
693    public boolean isEditProperty() {
694
695        return CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_editPropertyId);
696    }
697
698    /**
699     * @see java.lang.Object#toString()
700     */
701    @Override
702    public String toString() {
703
704        return getId() + " (" + ReflectionToStringBuilder.toString(this) + ")";
705    }
706
707}