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.dialogs.history.diff;
029
030import org.opencms.file.CmsObject;
031import org.opencms.ui.CmsVaadinUtils;
032import org.opencms.ui.Messages;
033import org.opencms.ui.components.OpenCmsTheme;
034import org.opencms.ui.util.table.Column;
035import org.opencms.util.CmsDateUtil;
036import org.opencms.util.CmsStringUtil;
037import org.opencms.workplace.comparison.CmsElementComparison;
038import org.opencms.workplace.comparison.CmsResourceComparison;
039import org.opencms.workplace.comparison.CmsXmlContentElementComparison;
040import org.opencms.xml.types.CmsXmlDateTimeValue;
041
042import java.text.DateFormat;
043import java.util.Date;
044
045import com.vaadin.ui.Button;
046import com.vaadin.ui.themes.ValoTheme;
047
048/**
049 * Represents a row of the XML content value comparison table.<p>
050 */
051public class CmsValueCompareBean {
052
053    /** The CMS context. */
054    private CmsObject m_cms;
055
056    /** The element comparison. */
057    private CmsElementComparison m_elemComp;
058
059    /** The button representing the change type. */
060    private Button m_getChangeTypeButton;
061
062    /**
063     * Creates a new instance.<p>
064     *
065     * @param cms the CMS context
066     * @param elemComp the element comparison bean
067     */
068    public CmsValueCompareBean(CmsObject cms, CmsElementComparison elemComp) {
069        m_cms = cms;
070        m_elemComp = elemComp;
071
072        String changeType = m_elemComp.getStatus();
073        String key = null;
074        String style = null;
075        if (CmsResourceComparison.TYPE_ADDED.equals(changeType)) {
076            key = org.opencms.workplace.comparison.Messages.GUI_COMPARE_ADDED_0;
077            style = OpenCmsTheme.DIFF_TYPE_ADDED;
078        } else if (CmsResourceComparison.TYPE_REMOVED.equals(changeType)) {
079            key = org.opencms.workplace.comparison.Messages.GUI_COMPARE_REMOVED_0;
080            style = OpenCmsTheme.DIFF_TYPE_DELETED;
081        } else if (CmsResourceComparison.TYPE_CHANGED.equals(changeType)) {
082            key = org.opencms.workplace.comparison.Messages.GUI_COMPARE_CHANGED_0;
083            style = OpenCmsTheme.DIFF_TYPE_CHANGED;
084        } else {
085            key = org.opencms.workplace.comparison.Messages.GUI_COMPARE_UNCHANGED_0;
086            style = OpenCmsTheme.DIFF_TYPE_UNCHANGED;
087        }
088
089        Button result = new Button();
090        result.setCaption(CmsVaadinUtils.getMessageText(key));
091        result.addStyleName(ValoTheme.BUTTON_LINK);
092        result.addStyleName(style);
093        m_getChangeTypeButton = result;
094
095    }
096
097    /**
098     * Formats an xml content value string for display in the value comparison table.<p>
099     *
100     * @param cms the CMS context
101     * @param comparison the element comparison
102     * @param origValue the XML content value as a string
103     *
104     * @return the formatted string
105     */
106    public static String formatContentValueForDiffTable(
107        CmsObject cms,
108        CmsElementComparison comparison,
109        String origValue) {
110
111        String result = CmsStringUtil.substitute(CmsStringUtil.trimToSize(origValue, 60), "\n", "");
112
113        // formatting DateTime
114        if (comparison instanceof CmsXmlContentElementComparison) {
115            if (((CmsXmlContentElementComparison)comparison).getType().equals(CmsXmlDateTimeValue.TYPE_NAME)) {
116                if (CmsStringUtil.isNotEmpty(result)) {
117
118                    result = CmsDateUtil.getDateTime(
119                        new Date(Long.parseLong(result)),
120                        DateFormat.SHORT,
121                        cms.getRequestContext().getLocale());
122                }
123            }
124        }
125        return result;
126    }
127
128    /**
129     * Gets the change type, as a button.<p>
130     *
131     * @return the change type
132     */
133    @Column(header = Messages.GUI_HISTORY_DIALOG_COL_CHANGETYPE_0, order = 10)
134    public Button getChangeType() {
135
136        return m_getChangeTypeButton;
137    }
138
139    /**
140     * Gets the locale.<p>
141     *
142     * @return the locale
143     */
144    @Column(header = Messages.GUI_HISTORY_DIALOG_COL_LOCALE_0, order = 20)
145    public String getLocale() {
146
147        return m_elemComp.getLocale().toString();
148    }
149
150    /**
151     * Gets the value for the first version.<p>
152     *
153     * @return the value for the first version
154     */
155    @Column(header = "V1 (%(v1))", order = 40)
156    public String getV1() {
157
158        return CmsValueCompareBean.formatContentValueForDiffTable(m_cms, m_elemComp, m_elemComp.getVersion1());
159    }
160
161    /**
162     * Gets the value for the second version.<p>
163     *
164     * @return the value for the second version
165     */
166    @Column(header = "V2 (%(v2))", order = 50)
167    public String getV2() {
168
169        return CmsValueCompareBean.formatContentValueForDiffTable(m_cms, m_elemComp, m_elemComp.getVersion2());
170    }
171
172    /**
173     * Gets the element name.<p>
174     *
175     * @return the element name
176     */
177    @Column(header = Messages.GUI_HISTORY_DIALOG_COL_XPATH_0, order = 30)
178    public String getXPath() {
179
180        return m_elemComp.getName();
181    }
182
183}