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 GmbH & Co. KG, 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.workplace.comparison;
029
030import org.opencms.file.CmsResource;
031import org.opencms.i18n.CmsMessageContainer;
032import org.opencms.jsp.CmsJspActionElement;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.util.CmsStringUtil;
036import org.opencms.util.CmsUUID;
037import org.opencms.workplace.CmsWorkplaceSettings;
038import org.opencms.workplace.commons.CmsHistoryList;
039import org.opencms.workplace.list.A_CmsListDialog;
040import org.opencms.workplace.list.CmsListColumnAlignEnum;
041import org.opencms.workplace.list.CmsListColumnDefinition;
042import org.opencms.workplace.list.CmsListDefaultAction;
043import org.opencms.workplace.list.CmsListDirectAction;
044import org.opencms.workplace.list.CmsListItem;
045import org.opencms.workplace.list.CmsListItemDetails;
046import org.opencms.workplace.list.CmsListMetadata;
047import org.opencms.workplace.list.CmsListOrderEnum;
048
049import java.io.IOException;
050import java.util.ArrayList;
051import java.util.HashMap;
052import java.util.Iterator;
053import java.util.List;
054import java.util.Map;
055
056import javax.servlet.ServletException;
057import javax.servlet.http.HttpServletRequest;
058import javax.servlet.http.HttpServletResponse;
059import javax.servlet.jsp.PageContext;
060
061import org.apache.commons.logging.Log;
062
063/**
064 * List for property comparison including columns for property name and the values. <p>
065 *
066 * @since 6.0.0
067 */
068public class CmsPropertyComparisonList extends A_CmsListDialog {
069
070    /** view first file action constant. */
071    public static final String LIST_ACTION_VIEW1 = "v1";
072
073    /** view second file action constant. */
074    public static final String LIST_ACTION_VIEW2 = "v2";
075
076    /** list action id constant. */
077    public static final String LIST_COLUMN_ICON = "ci";
078
079    /** list column id constant. */
080    public static final String LIST_COLUMN_PROPERTY_NAME = "cp";
081
082    /** list action id constant. */
083    public static final String LIST_COLUMN_TYPE = "cz";
084
085    /** list column id constant. */
086    public static final String LIST_COLUMN_VERSION_1 = "cv";
087
088    /** list column id constant. */
089    public static final String LIST_COLUMN_VERSION_2 = "cw";
090
091    /** list default action id constant. */
092    public static final String LIST_DEFACTION_VIEW = "dv";
093
094    /** list independent action id constant. */
095    public static final String LIST_DETAIL_TYPE = "dt";
096
097    /** list independent action id constant. */
098    public static final String LIST_IACTION_SHOW = "is";
099
100    /** List id constant. */
101    public static final String LIST_ID = "hipcl";
102
103    /** request parameter indicating whether attributes, elements or properties are compared.<p> */
104    public static final String PARAM_COMPARE = "compare";
105
106    /** The maximum length of properties and attributes to be displayed.<p> */
107    protected static final int TRIM_AT_LENGTH = 60;
108
109    /** The log object for this class. */
110    private static final Log LOG = CmsLog.getLog(CmsPropertyComparisonList.class);
111
112    /** Parameter value for the structure id of the first file. */
113    private String m_paramId1;
114
115    /** Parameter value for the structure id of the second file. */
116    private String m_paramId2;
117
118    /** Parameter value for the version of the first file. */
119    private String m_paramVersion1;
120
121    /** Parameter value for the version of the second file. */
122    private String m_paramVersion2;
123
124    /** The first resource to compare. */
125    private CmsResource m_resource1;
126
127    /** The second resource to compare. */
128    private CmsResource m_resource2;
129
130    /** The type of the files. */
131    private int m_resourceType;
132
133    /**
134     * Public constructor.<p>
135     *
136     * @param jsp an initialized JSP action element
137     */
138    public CmsPropertyComparisonList(CmsJspActionElement jsp) {
139
140        this(LIST_ID, jsp);
141    }
142
143    /**
144     * Public constructor with JSP variables.<p>
145     *
146     * @param context the JSP page context
147     * @param req the JSP request
148     * @param res the JSP response
149     */
150    public CmsPropertyComparisonList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
151
152        this(new CmsJspActionElement(context, req, res));
153    }
154
155    /**
156     * Public constructor.<p>
157     *
158     * @param jsp an initialized JSP action element
159     * @param listId the id of the displayed list
160     * @param listName the name of the list
161     * @param sortedColId the a priory sorted column
162     * @param sortOrder the order of the sorted column
163     * @param searchableColId the column to search into
164     */
165    protected CmsPropertyComparisonList(
166        CmsJspActionElement jsp,
167        String listId,
168        CmsMessageContainer listName,
169        String sortedColId,
170        CmsListOrderEnum sortOrder,
171        String searchableColId) {
172
173        super(jsp, listId, listName, sortedColId, sortOrder, searchableColId);
174    }
175
176    /**
177     * Protected constructor.<p>
178     *
179     * @param listId the id of the specialized list
180     * @param jsp an initialized JSP action element
181     */
182    protected CmsPropertyComparisonList(String listId, CmsJspActionElement jsp) {
183
184        super(
185            jsp,
186            listId,
187            Messages.get().container(Messages.GUI_COMPARE_PROPERTIES_0),
188            LIST_COLUMN_PROPERTY_NAME,
189            CmsListOrderEnum.ORDER_ASCENDING,
190            null);
191    }
192
193    /**
194     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
195     */
196    @Override
197    public void executeListMultiActions() {
198
199        throwListUnsupportedActionException();
200    }
201
202    /**
203     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
204     */
205    @Override
206    public void executeListSingleActions() throws IOException, ServletException {
207
208        // forward to the edit module screen
209        Map<String, String[]> params = new HashMap<String, String[]>();
210        params.put(CmsHistoryList.PARAM_VERSION_1, new String[] {getParamVersion1()});
211        params.put(CmsHistoryList.PARAM_VERSION_2, new String[] {getParamVersion2()});
212        params.put(CmsHistoryList.PARAM_ID_1, new String[] {getParamId1()});
213        params.put(CmsHistoryList.PARAM_ID_2, new String[] {getParamId2()});
214        params.put(PARAM_COMPARE, new String[] {"properties"});
215        params.put(PARAM_RESOURCE, new String[] {getParamResource()});
216        // forward to the difference screen
217        getToolManager().jspForwardTool(this, "/history/comparison/difference", params);
218    }
219
220    /**
221     * Returns the paramId1.<p>
222     *
223     * @return the paramId1
224     */
225    public String getParamId1() {
226
227        return m_paramId1;
228    }
229
230    /**
231     * Returns the paramId2.<p>
232     *
233     * @return the paramId2
234     */
235    public String getParamId2() {
236
237        return m_paramId2;
238    }
239
240    /**
241     * Returns the paramNewversionid.<p>
242     *
243     * @return the paramNewversionid
244     */
245    public String getParamVersion1() {
246
247        return m_paramVersion1;
248    }
249
250    /**
251     * Returns the paramOldversionid.<p>
252     *
253     * @return the paramOldversionid
254     */
255    public String getParamVersion2() {
256
257        return m_paramVersion2;
258    }
259
260    /**
261     * Returns the resource 1.<p>
262     *
263     * @return the resource 1
264     */
265    public CmsResource getResource1() {
266
267        return m_resource1;
268    }
269
270    /**
271     * Returns the resource 2.<p>
272     *
273     * @return the resource 2
274     */
275    public CmsResource getResource2() {
276
277        return m_resource2;
278    }
279
280    /**
281     * Returns the resourceType.<p>
282     *
283     * @return the resourceType
284     */
285    public int getResourceType() {
286
287        return m_resourceType;
288    }
289
290    /**
291     * Sets the paramId1.<p>
292     *
293     * @param paramId1 the paramId1 to set
294     */
295    public void setParamId1(String paramId1) {
296
297        m_paramId1 = paramId1;
298    }
299
300    /**
301     * Sets the paramId2.<p>
302     *
303     * @param paramId2 the paramId2 to set
304     */
305    public void setParamId2(String paramId2) {
306
307        m_paramId2 = paramId2;
308    }
309
310    /**
311     * Sets the paramNewversionid.<p>
312     *
313     * @param paramNewversionid the paramNewversionid to set
314     */
315    public void setParamVersion1(String paramNewversionid) {
316
317        m_paramVersion1 = paramNewversionid;
318    }
319
320    /**
321     * Sets the paramOldversionid.<p>
322     *
323     * @param paramOldversionid the paramOldversionid to set
324     */
325    public void setParamVersion2(String paramOldversionid) {
326
327        m_paramVersion2 = paramOldversionid;
328    }
329
330    /**
331     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
332     */
333    @Override
334    protected void fillDetails(String detailId) {
335
336        // no details
337    }
338
339    /**
340     * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
341     */
342    @Override
343    protected List<CmsListItem> getListItems() throws CmsException {
344
345        List<CmsListItem> ret = new ArrayList<CmsListItem>();
346        Iterator<CmsAttributeComparison> diffs = CmsResourceComparison.compareProperties(
347            getCms(),
348            getResource1(),
349            getParamVersion1(),
350            getResource2(),
351            getParamVersion2()).iterator();
352        while (diffs.hasNext()) {
353            CmsAttributeComparison comparison = diffs.next();
354            CmsListItem item = getList().newItem(comparison.getName());
355            item.set(LIST_COLUMN_PROPERTY_NAME, comparison.getName());
356            item.set(LIST_COLUMN_VERSION_1, CmsStringUtil.trimToSize(comparison.getVersion1(), TRIM_AT_LENGTH));
357            item.set(LIST_COLUMN_VERSION_2, CmsStringUtil.trimToSize(comparison.getVersion2(), TRIM_AT_LENGTH));
358            if (CmsResourceComparison.TYPE_ADDED.equals(comparison.getStatus())) {
359                item.set(LIST_COLUMN_TYPE, key(Messages.GUI_COMPARE_ADDED_0));
360            } else if (CmsResourceComparison.TYPE_REMOVED.equals(comparison.getStatus())) {
361                item.set(LIST_COLUMN_TYPE, key(Messages.GUI_COMPARE_REMOVED_0));
362            } else if (CmsResourceComparison.TYPE_CHANGED.equals(comparison.getStatus())) {
363                item.set(LIST_COLUMN_TYPE, key(Messages.GUI_COMPARE_CHANGED_0));
364            } else {
365                if (!getList().getMetadata().getItemDetailDefinition(LIST_IACTION_SHOW).isVisible()) {
366                    // do not display entry
367                    continue;
368                } else {
369                    item.set(LIST_COLUMN_TYPE, key(Messages.GUI_COMPARE_UNCHANGED_0));
370                }
371            }
372            ret.add(item);
373        }
374        getList().getMetadata().getColumnDefinition(LIST_COLUMN_VERSION_1).setName(
375            Messages.get().container(
376                Messages.GUI_COMPARE_VERSION_1,
377                CmsHistoryListUtil.getDisplayVersion(getParamVersion1(), getLocale())));
378        getList().getMetadata().getColumnDefinition(LIST_COLUMN_VERSION_2).setName(Messages.get().container(
379            Messages.GUI_COMPARE_VERSION_1,
380            CmsHistoryListUtil.getDisplayVersion(getParamVersion2(), getLocale())));
381        return ret;
382    }
383
384    /**
385     *
386     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
387     */
388    @Override
389    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
390
391        super.initWorkplaceRequestValues(settings, request);
392        try {
393            m_resource1 = CmsResourceComparisonDialog.readResource(
394                getCms(),
395                new CmsUUID(getParamId1()),
396                getParamVersion1());
397            m_resource2 = CmsResourceComparisonDialog.readResource(
398                getCms(),
399                new CmsUUID(getParamId2()),
400                getParamVersion2());
401        } catch (CmsException e) {
402            LOG.error(e.getLocalizedMessage(), e);
403        }
404        m_resourceType = m_resource1.getTypeId();
405    }
406
407    /**
408     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
409     */
410    @Override
411    protected void setColumns(CmsListMetadata metadata) {
412
413        // create column for icon
414        CmsListColumnDefinition iconCol = new CmsListColumnDefinition(LIST_COLUMN_ICON);
415        iconCol.setName(Messages.get().container(Messages.GUI_COMPARE_COLS_ICON_0));
416        iconCol.setWidth("20");
417        iconCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
418        iconCol.setSorteable(true);
419
420        // add state error action
421        CmsListDirectAction addedAction = new CmsListDirectAction(CmsResourceComparison.TYPE_ADDED) {
422
423            /**
424             * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#isVisible()
425             */
426            @Override
427            public boolean isVisible() {
428
429                String type = getItem().get(LIST_COLUMN_TYPE).toString();
430                return key(Messages.GUI_COMPARE_ADDED_0).equals(type);
431            }
432        };
433        addedAction.setName(Messages.get().container(Messages.GUI_COMPARE_ELEM_ADDED_0));
434        addedAction.setIconPath("tools/ex_history/buttons/added.png");
435        addedAction.setEnabled(true);
436        iconCol.addDirectAction(addedAction);
437
438        // add state error action
439        CmsListDirectAction removedAction = new CmsListDirectAction(CmsResourceComparison.TYPE_REMOVED) {
440
441            /**
442             * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#isVisible()
443             */
444            @Override
445            public boolean isVisible() {
446
447                String type = getItem().get(LIST_COLUMN_TYPE).toString();
448                return key(Messages.GUI_COMPARE_REMOVED_0).equals(type);
449            }
450        };
451        removedAction.setName(Messages.get().container(Messages.GUI_COMPARE_ELEM_REMOVED_0));
452        removedAction.setIconPath("tools/ex_history/buttons/removed.png");
453        removedAction.setEnabled(true);
454        iconCol.addDirectAction(removedAction);
455
456        // add state error action
457        CmsListDirectAction changedAction = new CmsListDirectAction(CmsResourceComparison.TYPE_CHANGED) {
458
459            /**
460             * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#isVisible()
461             */
462            @Override
463            public boolean isVisible() {
464
465                String type = getItem().get(LIST_COLUMN_TYPE).toString();
466                return key(Messages.GUI_COMPARE_CHANGED_0).equals(type);
467            }
468        };
469        changedAction.setName(Messages.get().container(Messages.GUI_COMPARE_ELEM_CHANGED_0));
470        changedAction.setIconPath("tools/ex_history/buttons/changed.png");
471        changedAction.setEnabled(true);
472        iconCol.addDirectAction(changedAction);
473
474        // add state error action
475        CmsListDirectAction unchangedAction = new CmsListDirectAction(CmsResourceComparison.TYPE_UNCHANGED) {
476
477            /**
478             * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#isVisible()
479             */
480            @Override
481            public boolean isVisible() {
482
483                String type = getItem().get(LIST_COLUMN_TYPE).toString();
484                return key(Messages.GUI_COMPARE_UNCHANGED_0).equals(type);
485            }
486        };
487        unchangedAction.setName(Messages.get().container(Messages.GUI_COMPARE_ELEM_UNCHANGED_0));
488        unchangedAction.setIconPath("tools/ex_history/buttons/unchanged.png");
489        unchangedAction.setEnabled(true);
490        iconCol.addDirectAction(unchangedAction);
491        metadata.addColumn(iconCol);
492        iconCol.setPrintable(false);
493
494        // add column for type
495        CmsListColumnDefinition typeCol = new CmsListColumnDefinition(LIST_COLUMN_TYPE);
496        typeCol.setName(Messages.get().container(Messages.GUI_COMPARE_COLS_STATUS_0));
497        typeCol.setWidth("10%");
498        CmsListDefaultAction typeColAction = new CmsListDefaultAction(CmsElementComparisonList.LIST_ACTION_STATUS);
499        typeColAction.setName(Messages.get().container(Messages.GUI_COMPARE_COLS_STATUS_0));
500        typeColAction.setEnabled(true);
501        // set action for the name column
502        typeCol.addDefaultAction(typeColAction);
503        metadata.addColumn(typeCol);
504        typeCol.setPrintable(true);
505
506        // add column for name
507        CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_PROPERTY_NAME);
508        nameCol.setName(Messages.get().container(Messages.GUI_COMPARE_COLS_PROPERTY_NAME_0));
509        nameCol.setWidth("20%");
510        metadata.addColumn(nameCol);
511        nameCol.setPrintable(true);
512
513        // add column for first value
514        CmsListColumnDefinition version1Col = new CmsListColumnDefinition(LIST_COLUMN_VERSION_1);
515        version1Col.setName(
516            Messages.get().container(
517                Messages.GUI_COMPARE_VERSION_1,
518                CmsHistoryListUtil.getDisplayVersion(getParamVersion1(), getLocale())));
519        version1Col.setWidth("35%");
520        version1Col.setSorteable(false);
521        metadata.addColumn(version1Col);
522        version1Col.setPrintable(true);
523
524        // add column for second value
525        CmsListColumnDefinition version2Col = new CmsListColumnDefinition(LIST_COLUMN_VERSION_2);
526        version2Col.setName(
527            Messages.get().container(
528                Messages.GUI_COMPARE_VERSION_1,
529                CmsHistoryListUtil.getDisplayVersion(getParamVersion2(), getLocale())));
530        version2Col.setWidth("35%");
531        version2Col.setSorteable(false);
532        metadata.addColumn(version2Col);
533        version2Col.setPrintable(true);
534    }
535
536    /**
537     *
538     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
539     */
540    @Override
541    protected void setIndependentActions(CmsListMetadata metadata) {
542
543        // add event details
544        CmsListItemDetails eventDetails = new CmsListItemDetails(LIST_IACTION_SHOW);
545        eventDetails.setVisible(false);
546        eventDetails.setShowActionName(Messages.get().container(Messages.GUI_COMPARE_SHOW_ALL_PROPERTIES_0));
547        eventDetails.setHideActionName(Messages.get().container(Messages.GUI_COMPARE_HIDE_IDENTICAL_PROPERTIES_0));
548        metadata.addItemDetails(eventDetails);
549    }
550
551    /**
552     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
553     */
554    @Override
555    protected void setMultiActions(CmsListMetadata metadata) {
556
557        // no-op
558    }
559}