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.module;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsProperty;
032import org.opencms.file.CmsResource;
033import org.opencms.importexport.CmsImportVersion10.RelationData;
034import org.opencms.security.CmsAccessControlEntry;
035import org.opencms.util.CmsFileUtil;
036
037import java.io.File;
038import java.io.FileOutputStream;
039import java.util.ArrayList;
040import java.util.List;
041import java.util.Map;
042
043/**
044 * Import data for a single resource.<p>
045 */
046public class CmsResourceImportData {
047
048    /** The access control entries. */
049    private List<CmsAccessControlEntry> m_aces;
050
051    /** Flag indicating whether resource id checks should be skipped for this import resource. */
052    private boolean m_skipResourceIdCheck;
053
054    /** The temp file with the content (may be null). */
055    private File m_contentFile;
056
057    /** True if there is a modification date in the import. */
058    private boolean m_hasDateLastModified;
059
060    /** True if this had a structure id in the import. */
061    private boolean m_hasStructureId;
062
063    /** The import resource. */
064    private CmsResource m_importResource;
065
066    /** The path. */
067    private String m_path;
068
069    /** The properties. */
070    private List<CmsProperty> m_properties;
071
072    /** The relations. */
073    private List<RelationData> m_relationData;
074
075    /** The CmsResource object containing the attributes for the resource. */
076    private CmsResource m_resource;
077
078    /** The original type name from the manifest. */
079    private String m_typeName;
080
081    /**
082     * Creats a new instance.<p>
083     *
084     * @param resource the resource
085     * @param path the path
086     * @param content the content
087     * @param properties the properties
088     * @param aces the acccess control entries
089     * @param relationData the relation data
090     * @param hasStructureId true if has a structure id
091     * @param hasDateLastModified true if has a modification date
092     * @param typeName the type name from the manifest
093     */
094    public CmsResourceImportData(
095        CmsResource resource,
096        String path,
097        byte[] content,
098        List<CmsProperty> properties,
099        List<CmsAccessControlEntry> aces,
100        List<RelationData> relationData,
101        boolean hasStructureId,
102        boolean hasDateLastModified,
103        String typeName) {
104
105        m_typeName = typeName;
106        m_resource = resource;
107        m_path = path;
108        if (content != null) {
109            m_contentFile = createTempFile(content);
110        }
111
112        if (properties == null) {
113            properties = new ArrayList<>();
114        }
115        m_properties = properties;
116
117        if (aces == null) {
118            aces = new ArrayList<>();
119        }
120        m_aces = aces;
121
122        if (relationData == null) {
123            relationData = new ArrayList<>();
124        }
125        m_relationData = relationData;
126        m_hasStructureId = hasStructureId;
127        m_hasDateLastModified = hasDateLastModified;
128    }
129
130    /**
131     * Cleans up temp files.<p>
132     */
133    public void cleanUp() {
134
135        if (m_contentFile != null) {
136            m_contentFile.delete();
137        }
138    }
139
140    /**
141     * Computes the root path.<p>
142     *
143     * @param cms the CMS context
144     * @return the root path
145     */
146    public Object computeRootPath(CmsObject cms) {
147
148        return cms.getRequestContext().addSiteRoot(m_path);
149
150    }
151
152    /**
153     * Gets the access control entries.<p>
154     *
155     * @return the access control entries
156     */
157    public List<CmsAccessControlEntry> getAccessControlEntries() {
158
159        return m_aces;
160    }
161
162    /**
163     * Gets the content.<p>
164     *
165     * @return the content, or null if there is no content
166     */
167    public byte[] getContent() {
168
169        if (m_contentFile == null) {
170            return null;
171        }
172        try {
173            return CmsFileUtil.readFile(m_contentFile);
174        } catch (Exception e) {
175            throw new RuntimeException(e);
176        }
177    }
178
179    /**
180     * Gets the import resource.<p>
181     *
182     * This is set by the module updater if the resource has actually been  imported.
183     *
184     * @return the import resource
185     */
186    public CmsResource getImportResource() {
187
188        return m_importResource;
189    }
190
191    /**
192     * Gets the path.<p>
193     *
194     * @return the path
195     */
196    public String getPath() {
197
198        return m_path;
199    }
200
201    /**
202     * Gets the map of properties, with property names as keys.<p>
203     *
204     * @return the map of properties
205     */
206    public Map<String, CmsProperty> getProperties() {
207
208        return CmsProperty.getPropertyMap(m_properties);
209
210    }
211
212    /**
213     * Gets the relations.<p>
214     *
215     * @return the relations
216     */
217    public List<RelationData> getRelations() {
218
219        return m_relationData;
220    }
221
222    /**
223     * Gets the resource.<p>
224     *
225     * @return the resource
226     */
227    public CmsResource getResource() {
228
229        return m_resource;
230
231    }
232
233    /**
234     * Gets the original type name from the manifest.
235     *
236     * @return the type name
237     */
238    public String getTypeName() {
239
240        return m_typeName;
241    }
242
243    /**
244     * Checks if there is content.<p>
245     *
246     * @return true if there is content
247     */
248    public boolean hasContent() {
249
250        return m_contentFile != null;
251    }
252
253    /**
254     * Returns true if this had a modification date in the import.<p>
255     *
256     * @return true if this had a modification date in the import
257     */
258    public boolean hasDateLastModified() {
259
260        return m_hasDateLastModified;
261    }
262
263    /**
264     * Returns true if this had a structure id in the import.<p>
265     *
266     * @return true if this had a structure id in the import
267     */
268    public boolean hasStructureId() {
269
270        return m_hasStructureId;
271    }
272
273    /**
274     * Returns true if resource id checks should be disabled for this import resource.
275     *
276     * @return true if resource id checks should be disabled
277     */
278    public boolean isSkipResourceIdCheck() {
279
280        return m_skipResourceIdCheck;
281    }
282
283    /**
284     * Sets the import resource.<p>
285     *
286     * @param importRes the import resource
287     */
288    public void setImportResource(CmsResource importRes) {
289
290        m_importResource = importRes;
291    }
292
293    /**
294     * Sets the 'skip resource id check' flag.
295     *
296     * @param skipResourceIdCheck the new value
297     */
298    public void setSkipResourceIdCheck(boolean skipResourceIdCheck) {
299
300        m_skipResourceIdCheck = skipResourceIdCheck;
301    }
302
303    /**
304     * Creates a temp file to store the given content.<p>
305     *
306     * @param content the content to store in the temp file
307     *
308     * @return the created temp file
309     */
310    private File createTempFile(byte[] content) {
311
312        try {
313            File file = File.createTempFile("ocms-moduleresource-", ".dat");
314            file.deleteOnExit();
315            try (FileOutputStream output = new FileOutputStream(file)) {
316                output.write(content);
317            }
318            return file;
319        } catch (Exception e) {
320            throw new RuntimeException(e);
321        }
322    }
323
324}