001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.file.CmsResourceFilter;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.main.OpenCms;
036import org.opencms.ui.CmsVaadinUtils;
037import org.opencms.ui.I_CmsDialogContext;
038import org.opencms.ui.components.CmsBasicDialog;
039import org.opencms.ui.components.CmsOkCancelActionHandler;
040import org.opencms.workplace.commons.Messages;
041
042import java.util.ArrayList;
043import java.util.Collections;
044import java.util.List;
045
046import org.apache.commons.logging.Log;
047
048import com.vaadin.ui.Button;
049import com.vaadin.ui.Button.ClickEvent;
050import com.vaadin.ui.Button.ClickListener;
051import com.vaadin.ui.Panel;
052import com.vaadin.v7.ui.Label;
053import com.vaadin.v7.ui.VerticalLayout;
054
055/**
056 * The copy to project dialog.<p>
057 */
058public class CmsCopyToProjectDialog extends CmsBasicDialog {
059
060    /** Logger instance for this class. */
061    private static final Log LOG = CmsLog.getLog(CmsCopyToProjectDialog.class);
062
063    /** The serial version id. */
064    private static final long serialVersionUID = -3016972948701432951L;
065
066    /** the dialog context. */
067    private I_CmsDialogContext m_context;
068
069    /**
070     * Constructor.<p>
071     *
072     * @param context the dialog context
073     */
074    public CmsCopyToProjectDialog(I_CmsDialogContext context) {
075
076        m_context = context;
077        displayResourceInfo(m_context.getResources());
078        String projectName = m_context.getCms().getRequestContext().getCurrentProject().getName();
079        Panel resourceListPanel = null;
080        try {
081            List<String> projectRes = m_context.getCms().readProjectResources(
082                m_context.getCms().getRequestContext().getCurrentProject());
083            List<CmsResource> resources = new ArrayList<CmsResource>();
084            CmsObject rootCms = OpenCms.initCmsObject(m_context.getCms());
085            rootCms.getRequestContext().setSiteRoot("/");
086            for (String res : projectRes) {
087                try {
088                    CmsResource resource = rootCms.readResource(res, CmsResourceFilter.ONLY_VISIBLE_NO_DELETED);
089                    resources.add(resource);
090                } catch (CmsException ex) {
091                    LOG.warn("Error reading project resource '" + res + "'.", ex);
092                }
093            }
094            if (!resources.isEmpty()) {
095                resourceListPanel = createResourceListPanel(
096                    CmsVaadinUtils.getMessageText(Messages.GUI_COPYTOPROJECT_PART_1, projectName),
097                    resources);
098            }
099        } catch (CmsException e) {
100            LOG.error("Error reading resources of the project '" + projectName + "'.", e);
101        }
102        VerticalLayout main = new VerticalLayout();
103        if (resourceListPanel != null) {
104            main.addComponent(resourceListPanel);
105        }
106        Label label = new Label(
107            CmsVaadinUtils.getMessageText(
108                Messages.GUI_COPYTOPROJECT_PROJECT_CONFIRMATION_2,
109                m_context.getResources().get(0).getName(),
110                projectName));
111        main.addComponent(label);
112        main.setSpacing(true);
113        setContent(main);
114        Button okButton = createButtonOK();
115        okButton.addClickListener(new ClickListener() {
116
117            private static final long serialVersionUID = 1L;
118
119            public void buttonClick(ClickEvent event) {
120
121                submit();
122            }
123        });
124        addButton(okButton);
125
126        Button cancelButton = createButtonCancel();
127        cancelButton.addClickListener(new ClickListener() {
128
129            private static final long serialVersionUID = 1L;
130
131            public void buttonClick(ClickEvent event) {
132
133                cancel();
134            }
135        });
136        addButton(cancelButton);
137        setActionHandler(new CmsOkCancelActionHandler() {
138
139            private static final long serialVersionUID = 1L;
140
141            @Override
142            protected void cancel() {
143
144                CmsCopyToProjectDialog.this.cancel();
145            }
146
147            @Override
148            protected void ok() {
149
150                submit();
151            }
152        });
153    }
154
155    /**
156     * Cancels the dialog.<p>
157     */
158    void cancel() {
159
160        m_context.finish(null);
161    }
162
163    /**
164     * Submits the dialog.<p>
165     */
166    void submit() {
167
168        CmsResource target = m_context.getResources().get(0);
169        String resPath = m_context.getCms().getSitePath(target);
170        try {
171            m_context.getCms().copyResourceToProject(resPath);
172
173            m_context.finish(Collections.singletonList(target.getStructureId()));
174        } catch (CmsException e) {
175            m_context.error(e);
176        }
177    }
178}