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.workplace.comparison;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.file.CmsResourceFilter;
033import org.opencms.file.history.CmsHistoryResourceHandler;
034import org.opencms.main.CmsException;
035import org.opencms.main.CmsRuntimeException;
036import org.opencms.util.CmsUUID;
037import org.opencms.workplace.commons.Messages;
038
039import java.util.Locale;
040
041/**
042 * Utility methods for the history list.<p>
043 */
044public final class CmsHistoryListUtil {
045
046    /**
047     * Hidden default constructor.<p>
048     */
049    private CmsHistoryListUtil() {
050        // nothing
051    }
052
053    /**
054     * Returns the version number from a version parameter.<p>
055     *
056     * @param version might be negative for the online version
057     * @param locale if the result is for display purposes, the locale has to be <code>!= null</code>
058     *
059     * @return the display name
060     */
061    public static String getDisplayVersion(String version, Locale locale) {
062
063        int ver = Integer.parseInt(version);
064        if (ver == CmsHistoryResourceHandler.PROJECT_OFFLINE_VERSION) {
065            return Messages.get().getBundle(locale).key(Messages.GUI_PROJECT_OFFLINE_0);
066        }
067        if (ver < 0) {
068            ver *= -1;
069            if (locale != null) {
070                return Messages.get().getBundle(locale).key(Messages.GUI_PROJECT_ONLINE_1, Integer.valueOf(ver));
071            }
072        }
073        return "" + ver;
074    }
075
076    /**
077     * Returns the link to an historical file.<p>
078     *
079     * @param cms the cms context
080     * @param structureId the structure id of the file
081     * @param version the version number of the file
082     *
083     * @return the link to an historical file
084     */
085    public static String getHistoryLink(CmsObject cms, CmsUUID structureId, String version) {
086
087        String resourcePath;
088        CmsResource resource;
089        try {
090            resource = cms.readResource(structureId, CmsResourceFilter.ALL);
091            resourcePath = resource.getRootPath();
092        } catch (CmsException e) {
093            throw new CmsRuntimeException(e.getMessageContainer(), e);
094        }
095        StringBuffer link = new StringBuffer();
096        link.append(CmsHistoryResourceHandler.HISTORY_HANDLER);
097        link.append(resourcePath);
098        link.append('?');
099        link.append(CmsHistoryResourceHandler.PARAM_VERSION);
100        link.append('=');
101        link.append(CmsHistoryListUtil.getVersion("" + version));
102        return link.toString();
103    }
104
105    /**
106     * Returns the version number from a version parameter.<p>
107     *
108     * @param version might be negative for the online version
109     *
110     * @return the positive value
111     */
112    public static int getVersion(String version) {
113
114        int ver = Integer.parseInt(version);
115        return Math.abs(ver);
116    }
117
118}