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