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.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.v7.ui.Label;
052import com.vaadin.ui.Panel;
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        m_context = context;
076        displayResourceInfo(m_context.getResources());
077        String projectName = m_context.getCms().getRequestContext().getCurrentProject().getName();
078        Panel resourceListPanel = null;
079        try {
080            List<String> projectRes = m_context.getCms().readProjectResources(
081                m_context.getCms().getRequestContext().getCurrentProject());
082            List<CmsResource> resources = new ArrayList<CmsResource>();
083            CmsObject rootCms = OpenCms.initCmsObject(m_context.getCms());
084            rootCms.getRequestContext().setSiteRoot("/");
085            for (String res : projectRes) {
086                try {
087                    CmsResource resource = rootCms.readResource(res, CmsResourceFilter.ONLY_VISIBLE_NO_DELETED);
088                    resources.add(resource);
089                } catch (CmsException ex) {
090                    LOG.warn("Error reading project resource '" + res + "'.", ex);
091                }
092            }
093            if (!resources.isEmpty()) {
094                resourceListPanel = createResourceListPanel(
095                    CmsVaadinUtils.getMessageText(Messages.GUI_COPYTOPROJECT_PART_1, projectName),
096                    resources);
097            }
098        } catch (CmsException e) {
099            LOG.error("Error reading resources of the project '" + projectName + "'.", e);
100        }
101        VerticalLayout main = new VerticalLayout();
102        if (resourceListPanel != null) {
103            main.addComponent(resourceListPanel);
104        }
105        Label label = new Label(
106            CmsVaadinUtils.getMessageText(
107                Messages.GUI_COPYTOPROJECT_PROJECT_CONFIRMATION_2,
108                m_context.getResources().get(0).getName(),
109                projectName));
110        main.addComponent(label);
111        main.setSpacing(true);
112        setContent(main);
113        Button okButton = createButtonOK();
114        okButton.addClickListener(new ClickListener() {
115
116            private static final long serialVersionUID = 1L;
117
118            public void buttonClick(ClickEvent event) {
119
120                submit();
121            }
122        });
123        addButton(okButton);
124
125        Button cancelButton = createButtonCancel();
126        cancelButton.addClickListener(new ClickListener() {
127
128            private static final long serialVersionUID = 1L;
129
130            public void buttonClick(ClickEvent event) {
131
132                cancel();
133            }
134        });
135        addButton(cancelButton);
136        setActionHandler(new CmsOkCancelActionHandler() {
137
138            private static final long serialVersionUID = 1L;
139
140            @Override
141            protected void cancel() {
142
143                CmsCopyToProjectDialog.this.cancel();
144            }
145
146            @Override
147            protected void ok() {
148
149                submit();
150            }
151        });
152    }
153
154    /**
155     * Cancels the dialog.<p>
156     */
157    void cancel() {
158
159        m_context.finish(null);
160    }
161
162    /**
163     * Submits the dialog.<p>
164     */
165    void submit() {
166
167        CmsResource target = m_context.getResources().get(0);
168        String resPath = m_context.getCms().getSitePath(target);
169        try {
170            m_context.getCms().copyResourceToProject(resPath);
171
172            m_context.finish(Collections.singletonList(target.getStructureId()));
173        } catch (CmsException e) {
174            m_context.error(e);
175        }
176    }
177}