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.gwt.shared;
029
030import org.opencms.util.CmsStringUtil;
031
032import java.util.List;
033
034import com.google.gwt.user.client.rpc.IsSerializable;
035
036/**
037 * Bean representing a file version for the history dialog.<p>
038 *
039 * Since the history dialog can also display offline resources, or resources read from the Online project without a historical version id,
040 * this class is needed rather than a single integer to represent a version.<p>
041 *
042 */
043public class CmsHistoryVersion implements IsSerializable {
044
045    /**
046     * Enum for distinguishing between offline and online project.<p>
047     */
048    public enum OfflineOnline {
049        /** Offline project. */
050        offline,
051        /** Online project. */
052        online;
053    }
054
055    /** We could use a nullable Boolean here, but use a custom enum for clarity. */
056    private OfflineOnline m_offlineOnline;
057
058    /** The version number. */
059    private Integer m_versionNumber;
060
061    /**
062     * Creates a new instance.<p>
063     *
064     * @param versionNumber the version number
065     * @param offlineOnline the offline/online state
066     */
067    public CmsHistoryVersion(Integer versionNumber, OfflineOnline offlineOnline) {
068
069        m_offlineOnline = offlineOnline;
070        m_versionNumber = versionNumber;
071    }
072
073    /**
074     * Default constructor for serialization.<p>
075     */
076    protected CmsHistoryVersion() {
077
078        // Do nothing
079
080    }
081
082    /**
083     * Converts a string to a CmsHistoryVersion.<p>
084     *
085     * This is the inverse of toString().
086     *
087     * @param s the string from which to read the history version
088     *
089     * @return the history version
090     */
091    public static CmsHistoryVersion fromString(String s) {
092
093        List<String> l = CmsStringUtil.splitAsList(s, ":");
094        if (l.size() == 2) {
095
096            Integer ver = null;
097            try {
098                ver = Integer.valueOf(l.get(0));
099            } catch (Exception e) {
100                //
101            }
102            OfflineOnline onlineStatus = "null".equals("" + l.get(1)) ? null : OfflineOnline.valueOf(l.get(1));
103            return new CmsHistoryVersion(ver, onlineStatus);
104        }
105        return null;
106
107    }
108
109    /**
110     * Gets the version number, or null if no version number was set.<p>
111     *
112     * @return the version number
113     */
114    public Integer getVersionNumber() {
115
116        return m_versionNumber;
117    }
118
119    /**
120     * Returns true if this is the offline version.<p>
121     *
122     * @return true if this is the offline version
123     */
124    public boolean isOffline() {
125
126        return OfflineOnline.offline.equals(m_offlineOnline);
127    }
128
129    /**
130     * Returns true if this is the online version.<p>
131     *
132     * @return true if this is the online version
133     */
134    public boolean isOnline() {
135
136        return OfflineOnline.online.equals(m_offlineOnline);
137    }
138
139    /**
140     * @see java.lang.Object#toString()
141     */
142    @Override
143    public String toString() {
144
145        return m_versionNumber + ":" + m_offlineOnline;
146    }
147
148}