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.CmsProperty;
032import org.opencms.file.CmsPropertyDefinition;
033import org.opencms.file.CmsResource;
034import org.opencms.lock.CmsLockActionRecord;
035import org.opencms.lock.CmsLockActionRecord.LockChange;
036import org.opencms.lock.CmsLockException;
037import org.opencms.lock.CmsLockUtil;
038import org.opencms.main.CmsException;
039import org.opencms.main.CmsLog;
040import org.opencms.main.OpenCms;
041import org.opencms.site.CmsSite;
042import org.opencms.ui.A_CmsUI;
043import org.opencms.ui.CmsVaadinUtils;
044import org.opencms.ui.I_CmsDialogContext;
045import org.opencms.ui.components.CmsBasicDialog;
046import org.opencms.ui.components.CmsOkCancelActionHandler;
047import org.opencms.util.CmsUUID;
048import org.opencms.workplace.CmsWorkplaceMessages;
049
050import java.util.ArrayList;
051import java.util.Arrays;
052import java.util.List;
053import java.util.Map;
054
055import org.apache.commons.logging.Log;
056
057import com.vaadin.ui.Button;
058import com.vaadin.ui.Button.ClickEvent;
059import com.vaadin.ui.Button.ClickListener;
060import com.vaadin.ui.themes.ValoTheme;
061import com.vaadin.v7.data.Property.ValueChangeEvent;
062import com.vaadin.v7.data.Property.ValueChangeListener;
063import com.vaadin.v7.ui.CheckBox;
064import com.vaadin.v7.ui.Label;
065import com.vaadin.v7.ui.OptionGroup;
066import com.vaadin.v7.ui.TextField;
067
068/**
069 * Dialog used to change resource modification times.<p>
070 */
071public class CmsSecureExportDialog extends CmsBasicDialog {
072
073    /** Logger instance for this class. */
074    private static final Log LOG = CmsLog.getLog(CmsSecureExportDialog.class);
075
076    /** Serial version id. */
077    private static final long serialVersionUID = 1L;
078
079    /** The Cancel button. */
080    protected Button m_cancelButton;
081
082    /** The dialog context. */
083    protected I_CmsDialogContext m_context;
084
085    /** Field for the export setting. */
086    protected OptionGroup m_exportField;
087
088    /** Field for the export name. */
089    protected TextField m_exportNameField;
090
091    /** Field for the 'internal' option. */
092    protected CheckBox m_internalField;
093
094    /** The OK  button. */
095    protected Button m_okButton;
096
097    /** The current resource. */
098    protected CmsResource m_resource;
099
100    /** Field for the secure setting. */
101    protected OptionGroup m_secureField;
102
103    /** Label to inform user that server has no secure server. */
104    protected Label m_noSecureServerLabel;
105
106    /** The label to display the online link. */
107    private Label m_linkField;
108
109    /**
110     * Creates a new instance.<p>
111     *
112     * @param context the dialog context
113     */
114    public CmsSecureExportDialog(I_CmsDialogContext context) {
115
116        m_context = context;
117        CmsObject cms = context.getCms();
118
119        CmsVaadinUtils.readAndLocalizeDesign(
120            this,
121            OpenCms.getWorkplaceManager().getMessages(A_CmsUI.get().getLocale()),
122            null);
123        m_resource = m_context.getResources().get(0);
124        initOptionGroup(m_secureField);
125        initOptionGroup(m_exportField);
126        m_linkField.setValue(OpenCms.getLinkManager().getOnlineLink(cms, cms.getSitePath(m_resource)));
127
128        loadData();
129        CmsSite site = OpenCms.getSiteManager().getCurrentSite(context.getCms());
130        m_noSecureServerLabel.setVisible(false);
131        if ((site != null) && !site.hasSecureServer()) {
132            m_secureField.setEnabled(false);
133            m_secureField.setVisible(false);
134            m_noSecureServerLabel.setVisible(true);
135        }
136
137        m_internalField.addValueChangeListener(new ValueChangeListener() {
138
139            private static final long serialVersionUID = 1L;
140
141            public void valueChange(ValueChangeEvent event) {
142
143                Boolean valueObj = (Boolean)(event.getProperty().getValue());
144                if (valueObj.booleanValue()) {
145                    m_secureField.setEnabled(false);
146                    m_exportField.setEnabled(false);
147                } else {
148                    m_secureField.setEnabled(true);
149                    m_exportField.setEnabled(true);
150                }
151
152            }
153        });
154
155        m_cancelButton.addClickListener(new ClickListener() {
156
157            private static final long serialVersionUID = 1L;
158
159            public void buttonClick(ClickEvent event) {
160
161                cancel();
162            }
163
164        });
165
166        m_okButton.addClickListener(new ClickListener() {
167
168            private static final long serialVersionUID = 1L;
169
170            public void buttonClick(ClickEvent event) {
171
172                submit();
173            }
174        });
175        displayResourceInfo(m_context.getResources());
176
177        setActionHandler(new CmsOkCancelActionHandler() {
178
179            private static final long serialVersionUID = 1L;
180
181            @Override
182            protected void cancel() {
183
184                CmsSecureExportDialog.this.cancel();
185            }
186
187            @Override
188            protected void ok() {
189
190                submit();
191            }
192        });
193    }
194
195    /**
196     * Loads the dialog data.<p>
197     */
198    protected void loadData() {
199
200        try {
201            List<CmsProperty> propList = m_context.getCms().readPropertyObjects(m_resource, false);
202            List<CmsProperty> inheritedPropList = m_context.getCms().readPropertyObjects(m_resource, true);
203            Map<String, CmsProperty> propMap = CmsProperty.toObjectMap(propList);
204            Map<String, CmsProperty> inheritedPropMap = CmsProperty.toObjectMap(inheritedPropList);
205            String secureValue = convertPropertyToFieldValue(propMap.get(CmsPropertyDefinition.PROPERTY_SECURE));
206            String inheritedSecureValue = convertPropertyToFieldValue(
207                inheritedPropMap.get(CmsPropertyDefinition.PROPERTY_SECURE));
208
209            String exportValue = convertPropertyToFieldValue(propMap.get(CmsPropertyDefinition.PROPERTY_EXPORT));
210
211            CmsProperty exportnameProp = propMap.get(CmsPropertyDefinition.PROPERTY_EXPORTNAME);
212            String exportnameValue = "";
213            if (exportnameProp != null) {
214                exportnameValue = exportnameProp.getValue();
215            }
216            m_exportField.setValue(exportValue);
217            m_secureField.setValue(secureValue);
218            if ("".equals(secureValue) && !"".equals(inheritedSecureValue)) {
219                String origin = m_context.getCms().getRequestContext().removeSiteRoot(
220                    inheritedPropMap.get(CmsPropertyDefinition.PROPERTY_SECURE).getOrigin());
221                String inheritedValueCaption = CmsVaadinUtils.getMessageText(
222                    org.opencms.workplace.commons.Messages.GUI_SECURE_INHERIT_FROM_2,
223                    inheritedSecureValue,
224                    origin);
225                m_secureField.setItemCaption("", inheritedValueCaption);
226            }
227
228            m_exportNameField.setValue(exportnameValue);
229            m_internalField.setValue(Boolean.valueOf(m_resource.isInternal()));
230        } catch (CmsException e) {
231            m_context.error(e);
232
233        }
234    }
235
236    /**
237     * Touches the selected files.<p>
238     *
239     * @throws CmsException if something goes wrong
240     */
241    protected void saveData() throws CmsException {
242
243        CmsObject cms = m_context.getCms();
244        for (CmsResource resource : m_context.getResources()) {
245            CmsLockActionRecord actionRecord = null;
246            try {
247                actionRecord = CmsLockUtil.ensureLock(m_context.getCms(), resource);
248
249                String secureValue = (String)m_secureField.getValue();
250                String exportValue = (String)m_exportField.getValue();
251                String exportname = m_exportNameField.getValue();
252                CmsProperty secureProp = new CmsProperty(CmsPropertyDefinition.PROPERTY_SECURE, secureValue, null);
253                CmsProperty exportProp = new CmsProperty(CmsPropertyDefinition.PROPERTY_EXPORT, exportValue, null);
254                CmsProperty exportNameProp = new CmsProperty(
255                    CmsPropertyDefinition.PROPERTY_EXPORTNAME,
256                    exportname,
257                    null);
258                boolean internal = m_internalField.getValue().booleanValue();
259
260                cms.writePropertyObjects(resource, Arrays.asList(secureProp, exportProp, exportNameProp));
261                resource.setInternal(internal);
262                cms.writeResource(resource);
263            } finally {
264                if ((actionRecord != null) && (actionRecord.getChange() == LockChange.locked)) {
265                    try {
266                        cms.unlockResource(resource);
267                    } catch (CmsLockException e) {
268                        LOG.warn(e.getLocalizedMessage(), e);
269                    }
270                }
271            }
272        }
273    }
274
275    /**
276     * Cancels the dialog.<p>
277     */
278    void cancel() {
279
280        m_context.finish(new ArrayList<CmsUUID>());
281    }
282
283    /**
284     * Submits the dialog.<p>
285     */
286    void submit() {
287
288        try {
289            saveData();
290            m_context.finish(null);
291        } catch (Exception e) {
292            m_context.error(e);
293        }
294    }
295
296    /**
297     * Converts a property object to a field value for one of the boolean selection widgets.<p>
298     *
299     * @param prop the property to convert
300     * @return the field value
301     */
302    private String convertPropertyToFieldValue(CmsProperty prop) {
303
304        if (prop == null) {
305            return "";
306        }
307        return "" + Boolean.valueOf(prop.getValue());
308    }
309
310    /**
311     * Fills the selection widget with the options 'True', 'False' and 'Not set'.<p>
312     *
313     * @param optGroup the option group to initialize
314     */
315    private void initOptionGroup(OptionGroup optGroup) {
316
317        optGroup.addStyleName(ValoTheme.OPTIONGROUP_HORIZONTAL);
318        optGroup.setNullSelectionAllowed(false);
319        optGroup.addItem("true");
320        optGroup.addItem("false");
321        optGroup.addItem("");
322        CmsWorkplaceMessages wpMessages = OpenCms.getWorkplaceManager().getMessages(A_CmsUI.get().getLocale());
323        optGroup.setItemCaption("true", wpMessages.key(org.opencms.workplace.commons.Messages.GUI_LABEL_TRUE_0));
324        optGroup.setItemCaption("false", wpMessages.key(org.opencms.workplace.commons.Messages.GUI_LABEL_FALSE_0));
325        optGroup.setItemCaption("", wpMessages.key(org.opencms.workplace.commons.Messages.GUI_SECURE_NOT_SET_0));
326    }
327
328}