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