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.gwt.shared.property;
029
030import org.opencms.util.CmsUUID;
031
032import java.util.Map;
033
034import com.google.gwt.user.client.rpc.IsSerializable;
035
036/**
037 * A class which represents a property modification.<p>
038 *
039 * @since 8.0.0
040 */
041public class CmsPropertyModification implements IsSerializable {
042
043    /** The file name property name, use to edit the filename within a property dialog. */
044    public static final String FILE_NAME_PROPERTY = "~~~file_name_property~~~";
045
046    /** The resource id for which the property changed. */
047    private CmsUUID m_id;
048
049    /** A flag which indicates whether the structure value changed. */
050    private boolean m_isStructureValue;
051
052    /** The name of the property. */
053    private String m_name;
054
055    /** The new value. */
056    private String m_value;
057
058    /**
059    * Copy constructor.<p>
060    *
061    * @param propMod the modification bean from which to copy the data
062    */
063    public CmsPropertyModification(CmsPropertyModification propMod) {
064
065        m_id = propMod.m_id;
066        m_name = propMod.m_name;
067        m_value = propMod.m_value;
068        m_isStructureValue = propMod.m_isStructureValue;
069    }
070
071    /**
072     * Creates a new property modification bean.<p>
073     * @param resourceId the resource id for which the property changed
074     * @param propertyName the name of the property
075     * @param value the new property value
076     * @param isStructureValue flag which indicates whether the structure value changed
077     */
078    public CmsPropertyModification(CmsUUID resourceId, String propertyName, String value, boolean isStructureValue) {
079
080        m_id = resourceId;
081        m_name = propertyName;
082        m_value = value;
083        m_isStructureValue = isStructureValue;
084    }
085
086    /**
087     * Creates a new property modification bean.<p>
088     * @param path a path of the form id/propertyname/mode, where mode is either S for structure or R for resource
089     *
090     * @param value the new property value
091     */
092    public CmsPropertyModification(String path, String value) {
093
094        String[] pathComponents = path.split("/");
095        String idStr = pathComponents[0];
096        String propName = pathComponents[1];
097        String mode = pathComponents[2];
098        CmsUUID id = new CmsUUID(idStr);
099        m_id = id;
100        m_name = propName;
101        m_value = value;
102        m_isStructureValue = mode.equals(CmsClientProperty.PATH_STRUCTURE_VALUE);
103    }
104
105    /**
106      * Empty constructor for serialization.<p>
107      */
108    protected CmsPropertyModification() {
109
110        // empty constructor for serialization
111    }
112
113    /**
114     * Returns the id of the resource for which to change properties.<p>
115     *
116     * @return the id of ther resource for which to change properties
117     */
118    public CmsUUID getId() {
119
120        return m_id;
121    }
122
123    /**
124     * Returns the property name.<p>
125     *
126     * @return the property name
127     */
128    public String getName() {
129
130        return m_name;
131    }
132
133    /**
134     * Returns the new value.<p>
135     *
136     * @return the new value
137     */
138    public String getValue() {
139
140        return m_value;
141    }
142
143    /**
144     * Checks if this is the file name property.<p>
145     *
146     * @return <code>true</code> in case of the file name property
147     */
148    public boolean isFileNameProperty() {
149
150        return FILE_NAME_PROPERTY.equals(m_name);
151    }
152
153    /**
154     * Flag which indicates a structure value change.<p>
155     *
156     * @return true if the structure value was changed
157     */
158    public boolean isStructureValue() {
159
160        return m_isStructureValue;
161    }
162
163    /**
164     * Helper method for applying the change to a property map.<p>
165     *
166     * @param props a map of properties
167     */
168    public void updatePropertyInMap(Map<String, CmsClientProperty> props) {
169
170        CmsClientProperty prop = props.get(getName());
171        if (prop == null) {
172            prop = new CmsClientProperty(getName(), "", "");
173            props.put(getName(), prop);
174        }
175        if (isStructureValue()) {
176            prop.setStructureValue(getValue());
177        } else {
178            prop.setResourceValue(getValue());
179        }
180    }
181
182}