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