001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2007
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.components.service;
025:
026: import java.io.File;
027: import java.io.IOException;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.Map;
031:
032: import org.riotfamily.common.web.file.FileStore;
033: import org.riotfamily.common.web.file.FileStoreLocator;
034: import org.riotfamily.components.dao.ComponentDao;
035: import org.riotfamily.components.model.ComponentVersion;
036: import org.riotfamily.components.model.FileStorageInfo;
037:
038: /**
039: * @author Felix Gnass [fgnass at neteye dot de]
040: * @since 6.5
041: */
042: public class ComponentUpdate {
043:
044: private ComponentDao dao;
045:
046: private ComponentVersion version;
047:
048: private FileStoreLocator fileStoreLocator;
049:
050: private Map fileStores;
051:
052: public ComponentUpdate(ComponentDao dao, ComponentVersion version,
053: FileStoreLocator fileStoreLocator) {
054:
055: this .dao = dao;
056: this .version = version;
057: this .fileStoreLocator = fileStoreLocator;
058: }
059:
060: private FileStore getFileStore(String property) {
061: if (fileStores == null) {
062: fileStores = new HashMap();
063: Iterator it = dao.getFileStorageInfos(version.getType())
064: .iterator();
065: while (it.hasNext()) {
066: FileStorageInfo fsi = (FileStorageInfo) it.next();
067: FileStore fs = fileStoreLocator.getFileStore(fsi
068: .getFileStoreId());
069: fileStores.put(fsi.getProperty(), fs);
070: }
071: }
072: return (FileStore) fileStores.get(property);
073: }
074:
075: public String getString(String property) {
076: return version.getProperty(property);
077: }
078:
079: public void setString(String property, String value) {
080: version.setProperty(property, value);
081: }
082:
083: public File getFile(String property) {
084: String uri = getString(property);
085: if (uri == null) {
086: return null;
087: }
088: return getFileStore(property).retrieve(uri);
089: }
090:
091: public void setFile(String property, File file) throws IOException {
092: setFile(property, file, null);
093: }
094:
095: public void setFile(String property, File file, String fileStoreId)
096: throws IOException {
097:
098: FileStore fs = getFileStore(property);
099: if (fs == null) {
100: dao.saveFileStorageInfo(version.getType(), property,
101: fileStoreId);
102: fs = fileStoreLocator.getFileStore(fileStoreId);
103: }
104:
105: String oldUri = getString(property);
106: if (oldUri != null) {
107: fs.delete(oldUri);
108: }
109: String uri = null;
110: if (file != null) {
111: uri = fs.store(file, null);
112: }
113: setString(property, uri);
114: }
115:
116: }
|