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 -> {
122            if (getModuleName() != null) {
123                dialog.setModule(getModuleName(), CmsMoveResourceTypeDialog.this);
124            }
125        });
126        m_cancel.addClickListener(e -> CmsVaadinUtils.getWindow(CmsMoveResourceTypeDialog.this).close());
127    }
128
129    /**
130     * public constructor.<p>
131     *
132     * @param window window
133     * @param type resourcetype
134     */
135    public CmsMoveResourceTypeDialog(final Window window, I_CmsResourceType type) {
136
137        init(window);
138
139        m_type = type;
140
141        m_table.select(new CmsModuleRow(OpenCms.getModuleManager().getModule(type.getModuleName())));
142        m_table.setCurrentPageFirstItemId(new CmsModuleRow(OpenCms.getModuleManager().getModule(type.getModuleName())));
143
144        if (m_type instanceof CmsResourceTypeXmlContent) {
145            m_typeXML = (CmsResourceTypeXmlContent)m_type;
146            if (!OpenCms.getModuleManager().getModule(m_type.getModuleName()).getResources().contains(
147                m_typeXML.getSchema())) {
148                m_schemaOK = false;
149                m_ok.setEnabled(false);
150                m_moveAnyway.addValueChangeListener(new ValueChangeListener() {
151
152                    public void valueChange(ValueChangeEvent event) {
153
154                        setOkButton();
155
156                    }
157
158                });
159            }
160
161        }
162
163        CmsExplorerTypeSettings typeSetting = OpenCms.getWorkplaceManager().getExplorerTypeSetting(type.getTypeName());
164        displayResourceInfoDirectly(
165            Collections.singletonList(
166                new CmsResourceInfo(
167                    CmsVaadinUtils.getMessageText(typeSetting.getKey()),
168                    type.getModuleName(),
169                    CmsResourceUtil.getBigIconResource(typeSetting, null))));
170        m_ok.addClickListener(e -> updateResourceType(window));
171
172    }
173
174    /**
175     * Filters the table.<p>
176     *
177     * @param text to filter
178     */
179    protected void filterTable(String text) {
180
181        Container.Filterable container = (Container.Filterable)m_table.getContainerDataSource();
182        container.removeAllContainerFilters();
183        if (!CmsStringUtil.isEmptyOrWhitespaceOnly(text)) {
184            container.addContainerFilter(
185                new Or(
186                    new SimpleStringFilter("name", text, true, false),
187                    new SimpleStringFilter("title", text, true, false)));
188        }
189    }
190
191    /**
192     * Get the module name.<p>
193     *
194     * @return module name
195     */
196    protected String getModuleName() {
197
198        if (m_table.getValue() == null) {
199            return null;
200        }
201        return ((CmsModuleRow)m_table.getValue()).getName();
202    }
203
204    /**
205     * Set ok button.<p>
206     */
207    protected void setOkButton() {
208
209        m_ok.setEnabled(m_moveAnyway.getValue().booleanValue());
210    }
211
212    /**
213     * Update the resource type.<p>
214     *
215     * @param window
216     */
217    protected void updateResourceType(Window window) {
218
219        if (!((CmsModuleRow)m_table.getValue()).equals(
220            new CmsModuleRow(OpenCms.getModuleManager().getModule(m_type.getModuleName())))) {
221            CmsModule newModule = ((CmsModuleRow)m_table.getValue()).getModule().clone();
222            CmsModule oldModule = OpenCms.getModuleManager().getModule(m_type.getModuleName()).clone();
223
224            m_type.setModuleName(newModule.getName());
225
226            List<I_CmsResourceType> newTypes = Lists.newArrayList(newModule.getResourceTypes());
227            newTypes.add(m_type);
228            newModule.setResourceTypes(newTypes);
229            List<CmsExplorerTypeSettings> oldSettings = new ArrayList<CmsExplorerTypeSettings>(
230                oldModule.getExplorerTypes());
231            CmsExplorerTypeSettings settings = new CmsExplorerTypeSettings();
232
233            settings.setName(m_type.getTypeName());
234            settings = oldSettings.get(oldSettings.indexOf(settings));
235            oldSettings.remove(settings);
236            List<CmsExplorerTypeSettings> newSettings = new ArrayList<CmsExplorerTypeSettings>(
237                newModule.getExplorerTypes());
238            newSettings.add(settings);
239            oldModule.setExplorerTypes(oldSettings);
240            newModule.setExplorerTypes(newSettings);
241
242            List<I_CmsResourceType> oldTypes = Lists.newArrayList(oldModule.getResourceTypes());
243            oldTypes.remove(m_type);
244            oldModule.setResourceTypes(oldTypes);
245            if (m_schemaOK) {
246                List<String> oldResources = Lists.newArrayList(oldModule.getResources());
247                oldResources.remove(m_typeXML.getSchema());
248                oldModule.setResources(oldResources);
249
250                List<String> newResources = Lists.newArrayList(newModule.getResources());
251                newResources.add(m_typeXML.getSchema());
252                newModule.setResources(newResources);
253
254            }
255            try {
256                OpenCms.getModuleManager().updateModule(A_CmsUI.getCmsObject(), oldModule);
257                OpenCms.getModuleManager().updateModule(A_CmsUI.getCmsObject(), newModule);
258                OpenCms.getResourceManager().initialize(A_CmsUI.getCmsObject());
259                OpenCms.getWorkplaceManager().removeExplorerTypeSettings(oldModule);
260                OpenCms.getWorkplaceManager().addExplorerTypeSettings(newModule);
261                OpenCms.getWorkplaceManager().initialize(A_CmsUI.getCmsObject());
262
263            } catch (CmsException e) {
264                LOG.error("Unable to move resource type", e);
265            }
266
267        }
268        window.close();
269        A_CmsUI.get().reload();
270    }
271
272    /**
273     * Init the dialog.<p>
274     *
275     * @param window window
276     */
277    private void init(final Window window) {
278
279        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
280        m_warningIcon.setContentMode(ContentMode.HTML);
281        m_warningIcon.setValue(FontOpenCms.WARNING.getHtml());
282        if (window != null) {
283            m_cancel.addClickListener(e -> window.close());
284        }
285        m_table.setWidth("100%");
286        m_table.setHeight("100%");
287
288        List<CmsModuleRow> rows = new ArrayList<CmsModuleRow>();
289        for (CmsModule module : OpenCms.getModuleManager().getAllInstalledModules()) {
290            CmsModuleRow row = new CmsModuleRow(module);
291            rows.add(row);
292        }
293        CmsBeanTableBuilder<CmsModuleRow> builder = CmsBeanTableBuilder.newInstance(CmsModuleRow.class);
294        builder.buildTable(m_table, rows);
295        m_table.setCellStyleGenerator(builder.getDefaultCellStyleGenerator());
296        m_table.setItemIconPropertyId("icon");
297        m_table.setRowHeaderMode(RowHeaderMode.ICON_ONLY);
298        m_table.setSelectable(true);
299        m_table.setVisibleColumns("name", "title");
300        m_table.setSortContainerPropertyId("name");
301        m_table.sort();
302
303        m_filter.setIcon(FontOpenCms.FILTER);
304        m_filter.setInputPrompt(
305            Messages.get().getBundle(UI.getCurrent().getLocale()).key(Messages.GUI_EXPLORER_FILTER_0));
306        m_filter.addStyleName(ValoTheme.TEXTFIELD_INLINE_ICON);
307        m_filter.addTextChangeListener(new TextChangeListener() {
308
309            public void textChange(TextChangeEvent event) {
310
311                filterTable(event.getText());
312
313            }
314        });
315    }
316}