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