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.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.v7.data.Property.ValueChangeEvent;
058import com.vaadin.v7.data.Property.ValueChangeListener;
059import com.vaadin.ui.Button;
060import com.vaadin.ui.Button.ClickEvent;
061import com.vaadin.ui.Button.ClickListener;
062import com.vaadin.v7.ui.CheckBox;
063import com.vaadin.v7.ui.Label;
064import com.vaadin.v7.ui.OptionGroup;
065import com.vaadin.v7.ui.TextField;
066import com.vaadin.ui.themes.ValoTheme;
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        m_context = context;
116        CmsObject cms = context.getCms();
117
118        CmsVaadinUtils.readAndLocalizeDesign(
119            this,
120            OpenCms.getWorkplaceManager().getMessages(A_CmsUI.get().getLocale()),
121            null);
122        m_resource = m_context.getResources().get(0);
123        initOptionGroup(m_secureField);
124        initOptionGroup(m_exportField);
125        m_linkField.setValue(OpenCms.getLinkManager().getOnlineLink(cms, cms.getSitePath(m_resource)));
126
127        loadData();
128        CmsSite site = OpenCms.getSiteManager().getCurrentSite(context.getCms());
129        m_noSecureServerLabel.setVisible(false);
130        if ((site != null) && !site.hasSecureServer()) {
131            m_secureField.setEnabled(false);
132            m_secureField.setVisible(false);
133            m_noSecureServerLabel.setVisible(true);
134        }
135
136        m_internalField.addValueChangeListener(new ValueChangeListener() {
137
138            private static final long serialVersionUID = 1L;
139
140            public void valueChange(ValueChangeEvent event) {
141
142                Boolean valueObj = (Boolean)(event.getProperty().getValue());
143                if (valueObj.booleanValue()) {
144                    m_secureField.setEnabled(false);
145                    m_exportField.setEnabled(false);
146                } else {
147                    m_secureField.setEnabled(true);
148                    m_exportField.setEnabled(true);
149                }
150
151            }
152        });
153
154        m_cancelButton.addClickListener(new ClickListener() {
155
156            private static final long serialVersionUID = 1L;
157
158            public void buttonClick(ClickEvent event) {
159
160                cancel();
161            }
162
163        });
164
165        m_okButton.addClickListener(new ClickListener() {
166
167            private static final long serialVersionUID = 1L;
168
169            public void buttonClick(ClickEvent event) {
170
171                submit();
172            }
173        });
174        displayResourceInfo(m_context.getResources());
175
176        setActionHandler(new CmsOkCancelActionHandler() {
177
178            private static final long serialVersionUID = 1L;
179
180            @Override
181            protected void cancel() {
182
183                CmsSecureExportDialog.this.cancel();
184            }
185
186            @Override
187            protected void ok() {
188
189                submit();
190            }
191        });
192    }
193
194    /**
195     * Loads the dialog data.<p>
196     */
197    protected void loadData() {
198
199        try {
200            List<CmsProperty> propList = m_context.getCms().readPropertyObjects(m_resource, false);
201            List<CmsProperty> inheritedPropList = m_context.getCms().readPropertyObjects(m_resource, true);
202            Map<String, CmsProperty> propMap = CmsProperty.toObjectMap(propList);
203            Map<String, CmsProperty> inheritedPropMap = CmsProperty.toObjectMap(inheritedPropList);
204            String secureValue = convertPropertyToFieldValue(propMap.get(CmsPropertyDefinition.PROPERTY_SECURE));
205            String inheritedSecureValue = convertPropertyToFieldValue(
206                inheritedPropMap.get(CmsPropertyDefinition.PROPERTY_SECURE));
207
208            String exportValue = convertPropertyToFieldValue(propMap.get(CmsPropertyDefinition.PROPERTY_EXPORT));
209
210            CmsProperty exportnameProp = propMap.get(CmsPropertyDefinition.PROPERTY_EXPORTNAME);
211            String exportnameValue = "";
212            if (exportnameProp != null) {
213                exportnameValue = exportnameProp.getValue();
214            }
215            m_exportField.setValue(exportValue);
216            m_secureField.setValue(secureValue);
217            if ("".equals(secureValue) && !"".equals(inheritedSecureValue)) {
218                String origin = m_context.getCms().getRequestContext().removeSiteRoot(
219                    inheritedPropMap.get(CmsPropertyDefinition.PROPERTY_SECURE).getOrigin());
220                String inheritedValueCaption = CmsVaadinUtils.getMessageText(
221                    org.opencms.workplace.commons.Messages.GUI_SECURE_INHERIT_FROM_2,
222                    inheritedSecureValue,
223                    origin);
224                m_secureField.setItemCaption("", inheritedValueCaption);
225            }
226
227            m_exportNameField.setValue(exportnameValue);
228            m_internalField.setValue(Boolean.valueOf(m_resource.isInternal()));
229        } catch (CmsException e) {
230            m_context.error(e);
231
232        }
233    }
234
235    /**
236     * Touches the selected files.<p>
237     *
238     * @throws CmsException if something goes wrong
239     */
240    protected void saveData() throws CmsException {
241
242        CmsObject cms = m_context.getCms();
243        for (CmsResource resource : m_context.getResources()) {
244            CmsLockActionRecord actionRecord = null;
245            try {
246                actionRecord = CmsLockUtil.ensureLock(m_context.getCms(), resource);
247
248                String secureValue = (String)m_secureField.getValue();
249                String exportValue = (String)m_exportField.getValue();
250                String exportname = m_exportNameField.getValue();
251                CmsProperty secureProp = new CmsProperty(CmsPropertyDefinition.PROPERTY_SECURE, secureValue, null);
252                CmsProperty exportProp = new CmsProperty(CmsPropertyDefinition.PROPERTY_EXPORT, exportValue, null);
253                CmsProperty exportNameProp = new CmsProperty(
254                    CmsPropertyDefinition.PROPERTY_EXPORTNAME,
255                    exportname,
256                    null);
257                boolean internal = m_internalField.getValue().booleanValue();
258
259                cms.writePropertyObjects(resource, Arrays.asList(secureProp, exportProp, exportNameProp));
260                resource.setInternal(internal);
261                cms.writeResource(resource);
262            } finally {
263                if ((actionRecord != null) && (actionRecord.getChange() == LockChange.locked)) {
264                    try {
265                        cms.unlockResource(resource);
266                    } catch (CmsLockException e) {
267                        LOG.warn(e.getLocalizedMessage(), e);
268                    }
269                }
270            }
271        }
272    }
273
274    /**
275     * Cancels the dialog.<p>
276     */
277    void cancel() {
278
279        m_context.finish(new ArrayList<CmsUUID>());
280    }
281
282    /**
283     * Submits the dialog.<p>
284     */
285    void submit() {
286
287        try {
288            saveData();
289            m_context.finish(null);
290        } catch (Exception e) {
291            m_context.error(e);
292        }
293    }
294
295    /**
296     * Converts a property object to a field value for one of the boolean selection widgets.<p>
297     *
298     * @param prop the property to convert
299     * @return the field value
300     */
301    private String convertPropertyToFieldValue(CmsProperty prop) {
302
303        if (prop == null) {
304            return "";
305        }
306        return "" + Boolean.valueOf(prop.getValue());
307    }
308
309    /**
310     * Fills the selection widget with the options 'True', 'False' and 'Not set'.<p>
311     *
312     * @param optGroup the option group to initialize
313     */
314    private void initOptionGroup(OptionGroup optGroup) {
315
316        optGroup.addStyleName(ValoTheme.OPTIONGROUP_HORIZONTAL);
317        optGroup.setNullSelectionAllowed(false);
318        optGroup.addItem("true");
319        optGroup.addItem("false");
320        optGroup.addItem("");
321        CmsWorkplaceMessages wpMessages = OpenCms.getWorkplaceManager().getMessages(A_CmsUI.get().getLocale());
322        optGroup.setItemCaption("true", wpMessages.key(org.opencms.workplace.commons.Messages.GUI_LABEL_TRUE_0));
323        optGroup.setItemCaption("false", wpMessages.key(org.opencms.workplace.commons.Messages.GUI_LABEL_FALSE_0));
324        optGroup.setItemCaption("", wpMessages.key(org.opencms.workplace.commons.Messages.GUI_SECURE_NOT_SET_0));
325    }
326
327}