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;
029
030import org.opencms.ade.configuration.CmsResourceTypeConfig;
031import org.opencms.ade.containerpage.CmsAddDialogTypeHelper;
032import org.opencms.ade.galleries.shared.CmsResourceTypeBean;
033import org.opencms.file.CmsObject;
034import org.opencms.file.CmsResource;
035import org.opencms.lock.CmsLockActionRecord;
036import org.opencms.lock.CmsLockActionRecord.LockChange;
037import org.opencms.lock.CmsLockUtil;
038import org.opencms.main.CmsException;
039import org.opencms.main.CmsLog;
040import org.opencms.main.OpenCms;
041import org.opencms.ui.A_CmsUI;
042import org.opencms.ui.CmsVaadinUtils;
043import org.opencms.ui.I_CmsDialogContext;
044import org.opencms.workplace.explorer.CmsExplorerTypeSettings;
045
046import java.util.Arrays;
047
048import org.apache.commons.logging.Log;
049
050/**
051 * Dialog for changing the resource type.<p>
052 */
053public class CmsChangeTypeDialog extends CmsNewDialog {
054
055    /** Logger instance for this class. */
056    static final Log LOG = CmsLog.getLog(CmsChangeTypeDialog.class);
057
058    /** Serial version id. */
059    private static final long serialVersionUID = 1L;
060
061    /**
062     * Creates a new instance.<p>
063     *
064     * @param context the dialog context
065     *
066     * @throws CmsException if something goes wrong
067     */
068    public CmsChangeTypeDialog(I_CmsDialogContext context)
069    throws CmsException {
070
071        super(A_CmsUI.getCmsObject().readParentFolder(context.getResources().get(0).getStructureId()), context);
072        displayResourceInfo(context.getResources());
073        m_defaultLocationCheckbox.setVisible(false);
074    }
075
076    /**
077     * @see org.opencms.ui.dialogs.CmsNewDialog#handleSelection(org.opencms.ade.galleries.shared.CmsResourceTypeBean)
078     */
079    @Override
080    public void handleSelection(CmsResourceTypeBean typeBean) {
081
082        CmsResource changeRes = m_dialogContext.getResources().get(0);
083        CmsObject cms = A_CmsUI.getCmsObject();
084        CmsLockActionRecord lockRecord = null;
085        try {
086            lockRecord = CmsLockUtil.ensureLock(cms, changeRes);
087            cms.chtype(cms.getSitePath(changeRes), OpenCms.getResourceManager().getResourceType(typeBean.getType()));
088            m_dialogContext.finish(Arrays.asList(changeRes.getStructureId()));
089        } catch (CmsException e) {
090            m_dialogContext.error(e);
091        } finally {
092            if ((lockRecord != null) && (lockRecord.getChange() == LockChange.locked)) {
093                try {
094                    cms.unlockResource(changeRes);
095                    m_dialogContext.finish(Arrays.asList(changeRes.getStructureId()));
096                } catch (CmsException e) {
097                    LOG.error(e.getLocalizedMessage());
098                }
099            }
100        }
101
102    }
103
104    /**
105     * @see org.opencms.ui.dialogs.CmsNewDialog#createTypeHelper()
106     */
107    @Override
108    protected CmsAddDialogTypeHelper createTypeHelper() {
109
110        return new CmsAddDialogTypeHelper(CmsResourceTypeConfig.AddMenuType.workplace) {
111
112            @Override
113            protected boolean exclude(CmsResourceTypeBean type) {
114
115                boolean sameType = OpenCms.getResourceManager().matchResourceType(
116                    type.getType(),
117                    m_dialogContext.getResources().get(0).getTypeId());
118                if (sameType) {
119                    return true;
120                }
121
122                String typeName = type.getType();
123                try {
124                    boolean isFolder = m_dialogContext.getResources().get(0).isFolder();
125                    boolean identicalTypeGroup = OpenCms.getResourceManager().getResourceType(
126                        typeName).isFolder() == isFolder;
127                    return !identicalTypeGroup;
128                } catch (Exception e) {
129                    LOG.debug(e.getLocalizedMessage(), e);
130                    return false;
131                }
132            }
133        };
134    }
135
136    /**
137     *
138     * @see org.opencms.ui.dialogs.CmsNewDialog#getSubtitle(org.opencms.ade.galleries.shared.CmsResourceTypeBean, boolean)
139     */
140    @Override
141    protected String getSubtitle(CmsResourceTypeBean type, boolean useDefault) {
142
143        CmsExplorerTypeSettings explorerType = OpenCms.getWorkplaceManager().getExplorerTypeSetting(type.getType());
144        if (explorerType != null) {
145            String explorerInfo = explorerType.getInfo();
146            if (explorerInfo != null) {
147                return CmsVaadinUtils.getMessageText(explorerInfo);
148            }
149        }
150        return "";
151
152    }
153
154}