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 GmbH & Co. KG, 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.configuration;
029
030import org.opencms.file.CmsResource;
031import org.opencms.file.CmsResource.CmsResourceCopyMode;
032import org.opencms.file.types.A_CmsResourceType;
033import org.opencms.util.CmsMacroResolver;
034
035import java.io.Serializable;
036
037/**
038 * Describes a resource to copy during the creation of a new resource.<p>
039 *
040 * Usually used in folder types to copy some default resources to the folder,
041 * but also usable for file resources.<p>
042 *
043 * @since 6.0.0
044 */
045public class CmsConfigurationCopyResource implements Serializable {
046
047    /** Indicates "copy resources" should be copied with type {@link CmsResource#COPY_AS_NEW}. */
048    public static final String COPY_AS_NEW = "new";
049
050    /** Indicates "copy resources" should be copied with type {@link CmsResource#COPY_PRESERVE_SIBLING}. */
051    public static final String COPY_AS_PRESERVE = "preserve";
052
053    /** Indicates "copy resources" should be copied with type {@link CmsResource#COPY_AS_SIBLING}. */
054    public static final String COPY_AS_SIBLING = "sibling";
055
056    /** The serial version id. */
057    private static final long serialVersionUID = 3698327414935524194L;
058
059    /** The source resource. */
060    private String m_source;
061
062    /** The target resource (may contain macros). */
063    private String m_target;
064
065    /** Indicates that the original configured target was <code>null</code>.*/
066    private boolean m_targetWasNull;
067
068    /** The type of the copy, for example "as new", "as sibling" etc.*/
069    private CmsResourceCopyMode m_type;
070
071    /** Indicates that the original configured type setting was <code>null</code>.*/
072    private boolean m_typeWasNull;
073
074    /**
075     * Creates a new copy resource info container.<p>
076     *
077     * If target is <code>null</code>, the macro {@link A_CmsResourceType#MACRO_RESOURCE_FOLDER_PATH} is used as default.
078     * If type is <code>null</code>, the copy type {@link CmsResource#COPY_AS_NEW} is used as default.<p>
079     *
080     * @param source the source resource
081     * @param target the target resource (may contain macros)
082     * @param type the type of the copy, for example "as new", "as sibling" etc
083     */
084    public CmsConfigurationCopyResource(String source, String target, String type) {
085
086        m_source = source;
087
088        if (target == null) {
089            m_target = CmsMacroResolver.formatMacro(A_CmsResourceType.MACRO_RESOURCE_FOLDER_PATH);
090            m_targetWasNull = true;
091        } else {
092            m_target = target;
093        }
094
095        m_type = CmsResource.COPY_AS_NEW;
096        if (type != null) {
097            if (type.equalsIgnoreCase(CmsConfigurationCopyResource.COPY_AS_SIBLING)) {
098                m_type = CmsResource.COPY_AS_SIBLING;
099            } else if (type.equalsIgnoreCase(CmsConfigurationCopyResource.COPY_AS_PRESERVE)) {
100                m_type = CmsResource.COPY_PRESERVE_SIBLING;
101            }
102        } else {
103            m_typeWasNull = true;
104        }
105    }
106
107    /**
108     * Returns the source resource.<p>
109     *
110     * @return the source resource
111     */
112    public String getSource() {
113
114        return m_source;
115    }
116
117    /**
118     * Returns the target resource (may contain macros).<p>
119     *
120     * @return the target resource (may contain macros)
121     */
122    public String getTarget() {
123
124        return m_target;
125    }
126
127    /**
128     * Returns the type of the copy, for example "as new", "as sibling" etc.<p>
129     *
130     * Possible types are {@link org.opencms.file.CmsResource#COPY_AS_NEW},
131     * {@link org.opencms.file.CmsResource#COPY_AS_SIBLING} and
132     * {@link org.opencms.file.CmsResource#COPY_PRESERVE_SIBLING}.<p>
133     *
134     * @return the type of the copy, for example "as new", "as sibling" etc
135     */
136    public CmsResourceCopyMode getType() {
137
138        return m_type;
139    }
140
141    /**
142     * Returns the copy type as String.<p>
143     *
144     * @see #getType()
145     *
146     * @return the copy type as String
147     */
148    public String getTypeString() {
149
150        if (CmsResource.COPY_AS_SIBLING == m_type) {
151            return CmsConfigurationCopyResource.COPY_AS_SIBLING;
152        } else if (CmsResource.COPY_PRESERVE_SIBLING == m_type) {
153            return CmsConfigurationCopyResource.COPY_AS_PRESERVE;
154        }
155        return CmsConfigurationCopyResource.COPY_AS_NEW;
156    }
157
158    /**
159     * Returns <code>true</code> if the original target configuration was <code>null</code>.<p>
160     *
161     * @return  <code>true</code> if the original target configuration was <code>null</code>
162     */
163    public boolean isTargetWasNull() {
164
165        return m_targetWasNull;
166    }
167
168    /**
169     * Returns <code>true</code> if the original type configuration was <code>null</code>.<p>
170     *
171     * @return  <code>true</code> if the original type configuration was <code>null</code>
172     */
173    public boolean isTypeWasNull() {
174
175        return m_typeWasNull;
176    }
177
178    /**
179     * @see java.lang.Object#toString()
180     */
181    @Override
182    public String toString() {
183
184        StringBuffer result = new StringBuffer();
185
186        result.append("[");
187        result.append(this.getClass().getName());
188        result.append(", source=");
189        result.append(getSource());
190        result.append(", target=");
191        result.append(getTarget());
192        result.append(", type=");
193        result.append(getTypeString());
194        result.append("]");
195
196        return result.toString();
197    }
198}