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.apps.resourcetypes;
029
030import org.opencms.file.types.CmsResourceTypeXmlContent;
031import org.opencms.file.types.I_CmsResourceType;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsLog;
034import org.opencms.main.OpenCms;
035import org.opencms.module.CmsModule;
036import org.opencms.ui.A_CmsUI;
037import org.opencms.ui.CmsVaadinUtils;
038import org.opencms.ui.FontOpenCms;
039import org.opencms.ui.apps.Messages;
040import org.opencms.ui.apps.modules.CmsModuleRow;
041import org.opencms.ui.components.CmsBasicDialog;
042import org.opencms.ui.components.CmsResourceInfo;
043import org.opencms.ui.util.table.CmsBeanTableBuilder;
044import org.opencms.util.CmsStringUtil;
045import org.opencms.workplace.explorer.CmsExplorerTypeSettings;
046import org.opencms.workplace.explorer.CmsResourceUtil;
047
048import java.util.ArrayList;
049import java.util.Collections;
050import java.util.List;
051
052import org.apache.commons.logging.Log;
053
054import com.google.gwt.thirdparty.guava.common.collect.Lists;
055import com.vaadin.ui.Button;
056import com.vaadin.ui.UI;
057import com.vaadin.ui.Window;
058import com.vaadin.ui.themes.ValoTheme;
059import com.vaadin.v7.data.Container;
060import com.vaadin.v7.data.Property.ValueChangeEvent;
061import com.vaadin.v7.data.Property.ValueChangeListener;
062import com.vaadin.v7.data.util.filter.Or;
063import com.vaadin.v7.data.util.filter.SimpleStringFilter;
064import com.vaadin.v7.event.FieldEvents.TextChangeEvent;
065import com.vaadin.v7.event.FieldEvents.TextChangeListener;
066import com.vaadin.v7.shared.ui.label.ContentMode;
067import com.vaadin.v7.ui.CheckBox;
068import com.vaadin.v7.ui.Label;
069import com.vaadin.v7.ui.Table;
070import com.vaadin.v7.ui.Table.RowHeaderMode;
071import com.vaadin.v7.ui.TextField;
072import com.vaadin.v7.ui.VerticalLayout;
073
074/**
075 * Class for a dialog to move resource types to modules.<p>
076 */
077public class CmsMoveResourceTypeDialog extends CmsBasicDialog {
078
079    /** The log instance for this class. */
080    private static final Log LOG = CmsLog.getLog(CmsMoveResourceTypeDialog.class);
081
082    /** vaadin component.*/
083    private Button m_ok;
084
085    /** vaadin component.*/
086    private Button m_cancel;
087
088    /** vaadin component.*/
089    private Table m_table;
090
091    /** vaadin component.*/
092    private CheckBox m_moveAnyway;
093
094    /** Vaadin vomponent.*/
095    private Label m_warningIcon;
096
097    /** vaadin component.*/
098    private TextField m_filter;
099
100    /** Is schema ok.*/
101    private boolean m_schemaOK = true;
102
103    /**Vaadin component. */
104    private VerticalLayout m_missingSchemaLayout;
105
106    /**resource type.*/
107    private I_CmsResourceType m_type;
108
109    /** type content.*/
110    private CmsResourceTypeXmlContent m_typeXML;
111
112    /**
113     * Public constructor.<p>
114     *
115     * @param dialog dialog
116     */
117    public CmsMoveResourceTypeDialog(CmsNewResourceTypeDialog dialog) {
118
119        init(null);
120        m_missingSchemaLayout.setVisible(false);
121        m_ok.addClickListener(e -> dialog.setModule(getModuleName(), CmsMoveResourceTypeDialog.this));
122        m_cancel.addClickListener(e -> CmsVaadinUtils.getWindow(CmsMoveResourceTypeDialog.this).close());
123    }
124
125    /**
126     * public constructor.<p>
127     *
128     * @param window window
129     * @param type resourcetype
130     */
131    public CmsMoveResourceTypeDialog(final Window window, I_CmsResourceType type) {
132
133        init(window);
134
135        m_type = type;
136
137        m_table.select(new CmsModuleRow(OpenCms.getModuleManager().getModule(type.getModuleName())));
138        m_table.setCurrentPageFirstItemId(new CmsModuleRow(OpenCms.getModuleManager().getModule(type.getModuleName())));
139
140        if (m_type instanceof CmsResourceTypeXmlContent) {
141            m_typeXML = (CmsResourceTypeXmlContent)m_type;
142            if (!OpenCms.getModuleManager().getModule(m_type.getModuleName()).getResources().contains(
143                m_typeXML.getSchema())) {
144                m_schemaOK = false;
145                m_ok.setEnabled(false);
146                m_moveAnyway.addValueChangeListener(new ValueChangeListener() {
147
148                    public void valueChange(ValueChangeEvent event) {
149
150                        setOkButton();
151
152                    }
153
154                });
155            }
156
157        }
158
159        CmsExplorerTypeSettings typeSetting = OpenCms.getWorkplaceManager().getExplorerTypeSetting(type.getTypeName());
160        displayResourceInfoDirectly(
161            Collections.singletonList(
162                new CmsResourceInfo(
163                    CmsVaadinUtils.getMessageText(typeSetting.getKey()),
164                    type.getModuleName(),
165                    CmsResourceUtil.getBigIconResource(typeSetting, null))));
166        m_ok.addClickListener(e -> updateResourceType(window));
167
168    }
169
170    /**
171     * Filters the table.<p>
172     *
173     * @param text to filter
174     */
175    protected void filterTable(String text) {
176
177        Container.Filterable container = (Container.Filterable)m_table.getContainerDataSource();
178        container.removeAllContainerFilters();
179        if (!CmsStringUtil.isEmptyOrWhitespaceOnly(text)) {
180            container.addContainerFilter(
181                new Or(
182                    new SimpleStringFilter("name", text, true, false),
183                    new SimpleStringFilter("title", text, true, false)));
184        }
185    }
186
187    /**
188     * Get the module name.<p>
189     *
190     * @return module name
191     */
192    protected String getModuleName() {
193
194        return ((CmsModuleRow)m_table.getValue()).getName();
195    }
196
197    /**
198     * Set ok button.<p>
199     */
200    protected void setOkButton() {
201
202        m_ok.setEnabled(m_moveAnyway.getValue().booleanValue());
203    }
204
205    /**
206     * Update the resource type.<p>
207     *
208     * @param window
209     */
210    protected void updateResourceType(Window window) {
211
212        if (!((CmsModuleRow)m_table.getValue()).equals(
213            new CmsModuleRow(OpenCms.getModuleManager().getModule(m_type.getModuleName())))) {
214            CmsModule newModule = ((CmsModuleRow)m_table.getValue()).getModule().clone();
215            CmsModule oldModule = OpenCms.getModuleManager().getModule(m_type.getModuleName()).clone();
216
217            m_type.setModuleName(newModule.getName());
218
219            List<I_CmsResourceType> newTypes = Lists.newArrayList(newModule.getResourceTypes());
220            newTypes.add(m_type);
221            newModule.setResourceTypes(newTypes);
222            List<CmsExplorerTypeSettings> oldSettings = new ArrayList<CmsExplorerTypeSettings>(
223                oldModule.getExplorerTypes());
224            CmsExplorerTypeSettings settings = new CmsExplorerTypeSettings();
225
226            settings.setName(m_type.getTypeName());
227            settings = oldSettings.get(oldSettings.indexOf(settings));
228            oldSettings.remove(settings);
229            List<CmsExplorerTypeSettings> newSettings = new ArrayList<CmsExplorerTypeSettings>(
230                newModule.getExplorerTypes());
231            newSettings.add(settings);
232            oldModule.setExplorerTypes(oldSettings);
233            newModule.setExplorerTypes(newSettings);
234
235            List<I_CmsResourceType> oldTypes = Lists.newArrayList(oldModule.getResourceTypes());
236            oldTypes.remove(m_type);
237            oldModule.setResourceTypes(oldTypes);
238            if (m_schemaOK) {
239                List<String> oldResources = Lists.newArrayList(oldModule.getResources());
240                oldResources.remove(m_typeXML.getSchema());
241                oldModule.setResources(oldResources);
242
243                List<String> newResources = Lists.newArrayList(newModule.getResources());
244                newResources.add(m_typeXML.getSchema());
245                newModule.setResources(newResources);
246
247            }
248            try {
249                OpenCms.getModuleManager().updateModule(A_CmsUI.getCmsObject(), oldModule);
250                OpenCms.getModuleManager().updateModule(A_CmsUI.getCmsObject(), newModule);
251                OpenCms.getResourceManager().initialize(A_CmsUI.getCmsObject());
252                OpenCms.getWorkplaceManager().removeExplorerTypeSettings(oldModule);
253                OpenCms.getWorkplaceManager().addExplorerTypeSettings(newModule);
254                OpenCms.getWorkplaceManager().initialize(A_CmsUI.getCmsObject());
255
256            } catch (CmsException e) {
257                LOG.error("Unable to move resource type", e);
258            }
259
260        }
261        window.close();
262        A_CmsUI.get().reload();
263    }
264
265    /**
266     * Init the dialog.<p>
267     *
268     * @param window window
269     */
270    private void init(final Window window) {
271
272        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
273        m_warningIcon.setContentMode(ContentMode.HTML);
274        m_warningIcon.setValue(FontOpenCms.WARNING.getHtml());
275        if (window != null) {
276            m_cancel.addClickListener(e -> window.close());
277        }
278        m_table.setWidth("100%");
279        m_table.setHeight("100%");
280
281        List<CmsModuleRow> rows = new ArrayList<CmsModuleRow>();
282        for (CmsModule module : OpenCms.getModuleManager().getAllInstalledModules()) {
283            CmsModuleRow row = new CmsModuleRow(module);
284            rows.add(row);
285        }
286        CmsBeanTableBuilder<CmsModuleRow> builder = CmsBeanTableBuilder.newInstance(CmsModuleRow.class);
287        builder.buildTable(m_table, rows);
288        m_table.setCellStyleGenerator(builder.getDefaultCellStyleGenerator());
289        m_table.setItemIconPropertyId("icon");
290        m_table.setRowHeaderMode(RowHeaderMode.ICON_ONLY);
291        m_table.setSelectable(true);
292        m_table.setVisibleColumns("name", "title");
293        m_table.setSortContainerPropertyId("name");
294        m_table.sort();
295
296        m_filter.setIcon(FontOpenCms.FILTER);
297        m_filter.setInputPrompt(
298            Messages.get().getBundle(UI.getCurrent().getLocale()).key(Messages.GUI_EXPLORER_FILTER_0));
299        m_filter.addStyleName(ValoTheme.TEXTFIELD_INLINE_ICON);
300        m_filter.addTextChangeListener(new TextChangeListener() {
301
302            public void textChange(TextChangeEvent event) {
303
304                filterTable(event.getText());
305
306            }
307        });
308    }
309}