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.apps.dbmanager;
029
030import org.opencms.file.CmsPropertyDefinition;
031import org.opencms.main.CmsException;
032import org.opencms.main.CmsIllegalArgumentException;
033import org.opencms.ui.A_CmsUI;
034import org.opencms.ui.CmsVaadinUtils;
035import org.opencms.ui.apps.Messages;
036import org.opencms.ui.components.CmsBasicDialog;
037import org.opencms.util.CmsStringUtil;
038
039import com.vaadin.ui.Button;
040import com.vaadin.ui.Button.ClickEvent;
041import com.vaadin.ui.Button.ClickListener;
042import com.vaadin.ui.Window;
043import com.vaadin.v7.data.Validator;
044import com.vaadin.v7.ui.TextField;
045
046/**
047 * Class for dialog to create property definition.<p>
048 */
049public class CmsAddPropertyDefinitionDialog extends CmsBasicDialog {
050
051    /**Validator for new Property name. */
052    class PropertyExistValidator implements Validator {
053
054        /**vaadin serial id. */
055        private static final long serialVersionUID = -2500052661735259675L;
056
057        /**
058         * @see com.vaadin.data.Validator#validate(java.lang.Object)
059         */
060        public void validate(Object value) throws InvalidValueException {
061
062            if (value == null) {
063                throw new InvalidValueException(
064                    CmsVaadinUtils.getMessageText(Messages.GUI_DATABASEAPP_PROPERTY_VALIDATION_EMPTY_0));
065            }
066            String propName = (String)value;
067            if (CmsStringUtil.isEmptyOrWhitespaceOnly(propName)) {
068                throw new InvalidValueException(
069                    CmsVaadinUtils.getMessageText(Messages.GUI_DATABASEAPP_PROPERTY_VALIDATION_EMPTY_0));
070            }
071            try {
072                if (A_CmsUI.getCmsObject().readPropertyDefinition(propName) != null) {
073                    throw new InvalidValueException(
074                        CmsVaadinUtils.getMessageText(Messages.GUI_DATABASEAPP_PROPERTY_VALIDATION_ALREADY_EXIST_0));
075                }
076            } catch (CmsException e) {
077                //should not happen (non existing property leads to null, not throwing exception.)
078            }
079
080            try {
081                CmsPropertyDefinition.checkPropertyName(propName);
082            } catch (CmsIllegalArgumentException e) {
083                throw new InvalidValueException(
084                    CmsVaadinUtils.getMessageText(Messages.GUI_DATABASEAPP_PROPERTY_VALIDATION_NOTVALID_0));
085            }
086        }
087
088    }
089
090    /**vaadin serial id.*/
091    private static final long serialVersionUID = -5454565964997277536L;
092
093    /**New property name field. */
094    protected TextField m_newProperty;
095
096    /**ok button. */
097    private Button m_ok;
098
099    /**cancel button. */
100    private Button m_cancel;
101
102    /**
103     * public constructor.<p>
104     * @param window to be closed
105     * @param table to be updated
106     */
107    public CmsAddPropertyDefinitionDialog(final Window window, final CmsPropertyTable table) {
108
109        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
110
111        m_ok.addClickListener(new ClickListener() {
112
113            private static final long serialVersionUID = -7356827828386377748L;
114
115            public void buttonClick(ClickEvent event) {
116
117                m_newProperty.removeAllValidators();
118                m_newProperty.addValidator(new PropertyExistValidator());
119                if (m_newProperty.isValid()) {
120
121                    saveProperty();
122                    table.init();
123                    window.close();
124                }
125            }
126        });
127
128        m_cancel.addClickListener(new ClickListener() {
129
130            private static final long serialVersionUID = -3198675226086758775L;
131
132            public void buttonClick(ClickEvent event) {
133
134                window.close();
135
136            }
137        });
138
139    }
140
141    /**
142     * saves the new property.<p>
143     */
144    protected void saveProperty() {
145
146        try {
147            A_CmsUI.getCmsObject().createPropertyDefinition(m_newProperty.getValue());
148        } catch (CmsException e) {
149            //
150        }
151    }
152
153}