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.workplace.threads;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResourceFilter;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsIllegalArgumentException;
034import org.opencms.main.OpenCms;
035import org.opencms.util.CmsStringUtil;
036
037/**
038 * Settings object that provides the settings to repair XML content resources in the OpenCms virtual file system (VFS).<p>
039 *
040 * @since 6.2.0
041 */
042public final class CmsXmlContentRepairSettings {
043
044    /** Needed to verify if a VFS path String denotes a folder in VFS. */
045    private final CmsObject m_cms;
046
047    /** Flag indicating if to force the reparation. */
048    private boolean m_force;
049
050    /** Flag indicating if resources in sub folders should be repaired, too. */
051    private boolean m_includeSubFolders;
052
053    /** The resource type of the XML contents to process. */
054    private String m_resourceType;
055
056    /** The VFS folder of all XML content files to process. */
057    private String m_vfsFolder;
058
059    /**
060     * Default constructor with cms object that is used for VFS path validation.<p>
061     *
062     * @param cms the current users context to check the VFS path information
063     */
064    public CmsXmlContentRepairSettings(CmsObject cms) {
065
066        m_cms = cms;
067    }
068
069    /**
070     * Returns the resource type name of the XML contents to process.<p>
071     *
072     * @return the resource type name of the XML contents to process
073     */
074    public String getResourceType() {
075
076        return m_resourceType;
077    }
078
079    /**
080     * Returns the VFS folder under which XML contents will be processed recursively.<p>
081     *
082     * @return the VFS folder under which XML contents will be processed recursively
083     */
084    public String getVfsFolder() {
085
086        return m_vfsFolder;
087    }
088
089    /**
090     * Checks if to force the reparation.<p>
091     *
092     * @return <code>true</code> if to force the reparation
093     */
094    public boolean isForce() {
095
096        return m_force;
097    }
098
099    /**
100     * Returns the flag indicating if resources in sub folders should be repaired, too.<p>
101     *
102     * @return the flag indicating if resources in sub folders should be repaired, too
103     */
104    public boolean isIncludeSubFolders() {
105
106        return m_includeSubFolders;
107    }
108
109    /**
110     * Sets the force reparation flag.<p>
111     *
112     * @param force the force reparation flag to set
113     */
114    public void setForce(boolean force) {
115
116        m_force = force;
117    }
118
119    /**
120     * Sets the flag indicating if resources in sub folders should be repaired, too.<p>
121     *
122     * @param includeSubFolders the flag indicating if resources in sub folders should be repaired, too
123     */
124    public void setIncludeSubFolders(boolean includeSubFolders) {
125
126        m_includeSubFolders = includeSubFolders;
127    }
128
129    /**
130     * Sets the resource type name of the XML contents to process.<p>
131     *
132     * @param resourceType the resource type name of the XML contents to process
133     */
134    public void setResourceType(String resourceType) {
135
136        if (CmsStringUtil.isEmptyOrWhitespaceOnly(resourceType)) {
137            throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_VALUE_EMPTY_0));
138        }
139        m_resourceType = resourceType;
140    }
141
142    /**
143     * Sets the VFS folder under which XML contents will be processed recursively.<p>
144     *
145     * @param vfsFolder the VFS folder under which XML contents will be processed recursively
146     *
147     * @throws CmsIllegalArgumentException if the given VFS path is not valid
148     */
149    public void setVfsFolder(String vfsFolder) throws CmsIllegalArgumentException {
150
151        if (CmsStringUtil.isEmptyOrWhitespaceOnly(vfsFolder)) {
152            throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_VALUE_EMPTY_0));
153        }
154        // test if it is a valid path
155        if (!m_cms.existsResource(vfsFolder, CmsResourceFilter.ALL.addRequireFolder())) {
156            throw new CmsIllegalArgumentException(
157                Messages.get().container(Messages.ERR_XMLCONTENT_VFSFOLDER_1, vfsFolder));
158        }
159        m_vfsFolder = vfsFolder;
160    }
161
162    /**
163     * Returns the resource type ID of the XML contents to process.<p>
164     *
165     * @return the resource type ID of the XML contents to process
166     */
167    protected int getResourceTypeId() {
168
169        if (CmsStringUtil.isNotEmpty(getResourceType())) {
170            try {
171                return OpenCms.getResourceManager().getResourceType(getResourceType()).getTypeId();
172            } catch (CmsException e) {
173                // ignore, no valid resource type ID found
174            }
175        }
176        return -1;
177    }
178}