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.configuration.preferences;
029
030import org.opencms.db.CmsUserSettings;
031import org.opencms.file.CmsObject;
032import org.opencms.file.CmsProject;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.main.OpenCms;
036import org.opencms.xml.content.CmsXmlContentProperty;
037
038import java.util.Collections;
039import java.util.Iterator;
040import java.util.List;
041import java.util.Locale;
042
043import org.apache.commons.logging.Log;
044
045/**
046 * Preference subclass for selecting the start project.<p>
047 */
048public class CmsProjectPreference extends CmsBuiltinPreference {
049
050    /** The nice name. */
051    private static final String NICE_NAME = "%(key."
052        + org.opencms.workplace.commons.Messages.GUI_PREF_STARTUP_PROJECT_0
053        + ")";
054
055    /** The log object for this class. */
056    private static final Log LOG = CmsLog.getLog(CmsProjectPreference.class);
057
058    /**
059     * Creates a new instance.<p>
060     *
061     * @param name the name of the project
062     */
063    public CmsProjectPreference(String name) {
064
065        super(name);
066        m_basic = false;
067    }
068
069    /**
070     * @see org.opencms.configuration.preferences.CmsBuiltinPreference#getPropertyDefinition(org.opencms.file.CmsObject)
071     */
072    @Override
073    public CmsXmlContentProperty getPropertyDefinition() {
074
075        CmsXmlContentProperty prop = new CmsXmlContentProperty(
076            getName(), //name
077            "string", //type
078            null, //widget
079            null, //widgetconfig
080            null, //regex
081            null, //ruletype
082            null, //default
083            NICE_NAME, //nicename
084            null, //description
085            null, //error
086            null//preferfolder
087        );
088        return prop;
089    }
090
091    /**
092     * @see org.opencms.configuration.preferences.CmsBuiltinPreference#getPropertyDefinition(org.opencms.file.CmsObject)
093     */
094    @Override
095    public CmsXmlContentProperty getPropertyDefinition(CmsObject cms) {
096
097        Locale locale = OpenCms.getWorkplaceManager().getWorkplaceLocale(cms);
098        CmsXmlContentProperty prop = new CmsXmlContentProperty(
099            getName(), //name
100            "string", //type
101            "select_notnull", //widget
102            getProjectSelectOptions(cms, locale), //widgetconfig
103            null, //regex
104            null, //ruletype
105            null, //default
106            NICE_NAME, //nicename
107            null, //description
108            null, //error
109            null//preferfolder)
110        );
111        return prop;
112    }
113
114    /**
115     * Gets the options for the project selector.<p>
116     *
117     * @param cms  the CMS context
118     * @param  locale the locale
119     *
120     * @return the options for the project selector
121     */
122    private String getProjectSelectOptions(CmsObject cms, Locale locale) {
123
124        List<CmsProject> allProjects;
125        try {
126            String ouFqn = "";
127            CmsUserSettings settings = new CmsUserSettings(cms);
128            if (!settings.getListAllProjects()) {
129                ouFqn = cms.getRequestContext().getCurrentUser().getOuFqn();
130            }
131            allProjects = OpenCms.getOrgUnitManager().getAllAccessibleProjects(
132                cms,
133                ouFqn,
134                settings.getListAllProjects());
135        } catch (CmsException e) {
136            // should usually never happen
137            if (LOG.isErrorEnabled()) {
138                LOG.error(e.getLocalizedMessage(), e);
139            }
140            allProjects = Collections.emptyList();
141        }
142
143        boolean singleOu = true;
144        String ouFqn = null;
145        Iterator<CmsProject> itProjects = allProjects.iterator();
146        while (itProjects.hasNext()) {
147            CmsProject prj = itProjects.next();
148            if (prj.isOnlineProject()) {
149                // skip the online project
150                continue;
151            }
152            if (ouFqn == null) {
153                // set the first ou
154                ouFqn = prj.getOuFqn();
155            }
156            if (!ouFqn.equals(prj.getOuFqn())) {
157                // break if one different ou is found
158                singleOu = false;
159                break;
160            }
161        }
162
163        int counter = 0;
164        StringBuffer resultBuffer = new StringBuffer();
165        for (int i = 0, n = allProjects.size(); i < n; i++) {
166            CmsProject project = allProjects.get(i);
167            String projectName = project.getSimpleName();
168            if (!singleOu && !project.isOnlineProject()) {
169                try {
170                    projectName = projectName
171                        + " - "
172                        + OpenCms.getOrgUnitManager().readOrganizationalUnit(cms, project.getOuFqn()).getDisplayName(
173                            locale);
174                } catch (CmsException e) {
175                    projectName = projectName + " - " + project.getOuFqn();
176                }
177            }
178            if (counter != 0) {
179                resultBuffer.append("|");
180            }
181            counter++;
182            resultBuffer.append(project.getName()).append(":").append(projectName);
183        }
184        return resultBuffer.toString();
185    }
186
187}