001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.imagegallery.lar;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.lar.PortletDataContext;
026: import com.liferay.portal.kernel.lar.PortletDataException;
027: import com.liferay.portal.kernel.lar.PortletDataHandler;
028: import com.liferay.portal.kernel.lar.PortletDataHandlerBoolean;
029: import com.liferay.portal.kernel.lar.PortletDataHandlerControl;
030: import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
031: import com.liferay.portal.kernel.util.StringPool;
032: import com.liferay.portal.model.Image;
033: import com.liferay.portal.service.persistence.ImageUtil;
034: import com.liferay.portal.util.PortalUtil;
035: import com.liferay.portlet.imagegallery.NoSuchFolderException;
036: import com.liferay.portlet.imagegallery.NoSuchImageException;
037: import com.liferay.portlet.imagegallery.model.IGFolder;
038: import com.liferay.portlet.imagegallery.model.IGImage;
039: import com.liferay.portlet.imagegallery.model.impl.IGFolderImpl;
040: import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
041: import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
042: import com.liferay.portlet.imagegallery.service.persistence.IGFolderUtil;
043: import com.liferay.portlet.imagegallery.service.persistence.IGImageFinderUtil;
044: import com.liferay.portlet.imagegallery.service.persistence.IGImageUtil;
045: import com.liferay.util.CollectionFactory;
046: import com.liferay.util.FileUtil;
047: import com.liferay.util.MapUtil;
048:
049: import com.thoughtworks.xstream.XStream;
050:
051: import java.io.File;
052:
053: import java.util.ArrayList;
054: import java.util.Iterator;
055: import java.util.List;
056: import java.util.Map;
057:
058: import javax.portlet.PortletPreferences;
059:
060: import org.apache.commons.logging.Log;
061: import org.apache.commons.logging.LogFactory;
062:
063: import org.dom4j.Document;
064: import org.dom4j.DocumentHelper;
065: import org.dom4j.Element;
066:
067: /**
068: * <a href="IGPortletDataHandlerImpl.java.html"><b><i>View Source</i></b></a>
069: *
070: * @author Bruno Farache
071: *
072: */
073: public class IGPortletDataHandlerImpl implements PortletDataHandler {
074:
075: public PortletPreferences deleteData(PortletDataContext context,
076: String portletId, PortletPreferences prefs)
077: throws PortletDataException {
078:
079: try {
080:
081: // Folders
082:
083: if (!context.addPrimaryKey(IGPortletDataHandlerImpl.class,
084: "deleteData")) {
085:
086: IGFolderLocalServiceUtil.deleteFolders(context
087: .getGroupId());
088: }
089:
090: return null;
091: } catch (Exception e) {
092: throw new PortletDataException(e);
093: }
094: }
095:
096: public String exportData(PortletDataContext context,
097: String portletId, PortletPreferences prefs)
098: throws PortletDataException {
099:
100: try {
101: XStream xStream = new XStream();
102:
103: Document doc = DocumentHelper.createDocument();
104:
105: Element root = doc.addElement("image-gallery");
106:
107: root.addAttribute("group-id", String.valueOf(context
108: .getGroupId()));
109:
110: // Folders
111:
112: List folders = IGFolderUtil.findByGroupId(context
113: .getGroupId());
114:
115: List igImages = new ArrayList();
116:
117: Iterator itr = folders.iterator();
118:
119: while (itr.hasNext()) {
120: IGFolder folder = (IGFolder) itr.next();
121:
122: if (context.addPrimaryKey(IGFolder.class, folder
123: .getPrimaryKeyObj())) {
124:
125: itr.remove();
126: } else {
127: folder.setUserUuid(folder.getUserUuid());
128:
129: List folderIGImages = IGImageUtil
130: .findByFolderId(folder.getFolderId());
131:
132: igImages.addAll(folderIGImages);
133: }
134: }
135:
136: String xml = xStream.toXML(folders);
137:
138: Element el = root.addElement("ig-folders");
139:
140: Document tempDoc = PortalUtil.readDocumentFromXML(xml);
141:
142: el.content().add(tempDoc.getRootElement().createCopy());
143:
144: // IGImages
145:
146: itr = igImages.iterator();
147:
148: while (itr.hasNext()) {
149: IGImage igImage = (IGImage) itr.next();
150:
151: if (context.addPrimaryKey(IGImage.class, igImage
152: .getPrimaryKeyObj())) {
153:
154: itr.remove();
155: } else {
156: igImage.setUserUuid(igImage.getUserUuid());
157:
158: if (context.getBooleanParameter(_NAMESPACE, "tags")) {
159: context.addTagsEntries(IGImage.class, igImage
160: .getPrimaryKeyObj());
161: }
162:
163: try {
164: Image largeImage = ImageUtil
165: .findByPrimaryKey(igImage
166: .getLargeImageId());
167:
168: igImage.setImageType(largeImage.getType());
169:
170: context.getZipWriter().addEntry(
171: getIGImageDir(igImage),
172: largeImage.getTextObj());
173: } catch (com.liferay.portal.NoSuchImageException nsie) {
174: }
175: }
176: }
177:
178: xml = xStream.toXML(igImages);
179:
180: el = root.addElement("ig-images");
181:
182: tempDoc = PortalUtil.readDocumentFromXML(xml);
183:
184: el.content().add(tempDoc.getRootElement().createCopy());
185:
186: return doc.asXML();
187: } catch (Exception e) {
188: throw new PortletDataException(e);
189: }
190: }
191:
192: public PortletDataHandlerControl[] getExportControls()
193: throws PortletDataException {
194:
195: return new PortletDataHandlerControl[] { _foldersAndImages,
196: _tags };
197: }
198:
199: public PortletDataHandlerControl[] getImportControls()
200: throws PortletDataException {
201:
202: return new PortletDataHandlerControl[] { _foldersAndImages,
203: _tags };
204: }
205:
206: public PortletPreferences importData(PortletDataContext context,
207: String portletId, PortletPreferences prefs, String data)
208: throws PortletDataException {
209:
210: try {
211: XStream xStream = new XStream();
212:
213: Document doc = PortalUtil.readDocumentFromXML(data);
214:
215: Element root = doc.getRootElement();
216:
217: // Folders
218:
219: Element el = root.element("ig-folders").element("list");
220:
221: Document tempDoc = DocumentHelper.createDocument();
222:
223: tempDoc.content().add(el.createCopy());
224:
225: Map folderPKs = CollectionFactory.getHashMap();
226:
227: List folders = (List) xStream.fromXML(tempDoc.asXML());
228:
229: Iterator itr = folders.iterator();
230:
231: while (itr.hasNext()) {
232: IGFolder folder = (IGFolder) itr.next();
233:
234: importFolder(context, folderPKs, folder);
235: }
236:
237: // IGImages
238:
239: el = root.element("ig-images").element("list");
240:
241: tempDoc = DocumentHelper.createDocument();
242:
243: tempDoc.content().add(el.createCopy());
244:
245: List igImages = (List) xStream.fromXML(tempDoc.asXML());
246:
247: itr = igImages.iterator();
248:
249: while (itr.hasNext()) {
250: IGImage igImage = (IGImage) itr.next();
251:
252: importIGImage(context, folderPKs, igImage);
253: }
254:
255: return null;
256: } catch (Exception e) {
257: throw new PortletDataException(e);
258: }
259: }
260:
261: protected String getIGImageDir(IGImage igImage)
262: throws PortalException, SystemException {
263:
264: return _IMAGE_GALLERY_FOLDER + igImage.getPrimaryKey() + "."
265: + igImage.getImageType();
266: }
267:
268: protected void importFolder(PortletDataContext context,
269: Map folderPKs, IGFolder folder) throws Exception {
270:
271: long userId = context.getUserId(folder.getUserUuid());
272: long plid = context.getPlid();
273: long parentFolderId = MapUtil.getLong(folderPKs, folder
274: .getParentFolderId(), folder.getParentFolderId());
275:
276: boolean addCommunityPermissions = true;
277: boolean addGuestPermissions = true;
278:
279: IGFolder existingFolder = null;
280:
281: try {
282: if (parentFolderId != IGFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
283: IGFolderUtil.findByPrimaryKey(parentFolderId);
284: }
285:
286: if (context.getDataStrategy().equals(
287: PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
288:
289: existingFolder = IGFolderUtil.fetchByUUID_G(folder
290: .getUuid(), context.getGroupId());
291:
292: if (existingFolder == null) {
293: existingFolder = IGFolderLocalServiceUtil
294: .addFolder(folder.getUuid(), userId, plid,
295: parentFolderId, folder.getName(),
296: folder.getDescription(),
297: addCommunityPermissions,
298: addGuestPermissions);
299: } else {
300: existingFolder = IGFolderLocalServiceUtil
301: .updateFolder(existingFolder.getFolderId(),
302: parentFolderId, folder.getName(),
303: folder.getDescription(), false);
304: }
305: } else {
306: existingFolder = IGFolderLocalServiceUtil.addFolder(
307: userId, plid, parentFolderId, folder.getName(),
308: folder.getDescription(),
309: addCommunityPermissions, addGuestPermissions);
310: }
311:
312: folderPKs.put(folder.getPrimaryKeyObj(), existingFolder
313: .getPrimaryKeyObj());
314: } catch (NoSuchFolderException nsfe) {
315: _log.error("Could not find the parent folder for folder "
316: + folder.getFolderId());
317: }
318: }
319:
320: protected void importIGImage(PortletDataContext context,
321: Map folderPKs, IGImage igImage) throws Exception {
322:
323: long userId = context.getUserId(igImage.getUserUuid());
324: long folderId = MapUtil.getLong(folderPKs, igImage
325: .getFolderId(), igImage.getFolderId());
326:
327: File imageFile = null;
328:
329: byte[] byteArray = context.getZipReader().getEntryAsByteArray(
330: getIGImageDir(igImage));
331:
332: if (byteArray == null) {
333: _log.error("Could not find image for IG image "
334: + igImage.getImageId());
335:
336: return;
337: } else {
338: imageFile = File.createTempFile(String.valueOf(igImage
339: .getPrimaryKey()), StringPool.PERIOD
340: + igImage.getImageType());
341:
342: FileUtil.write(imageFile, byteArray);
343: }
344:
345: String[] tagsEntries = null;
346:
347: if (context.getBooleanParameter(_NAMESPACE, "tags")) {
348: tagsEntries = context.getTagsEntries(IGImage.class, igImage
349: .getPrimaryKeyObj());
350: }
351:
352: boolean addCommunityPermissions = true;
353: boolean addGuestPermissions = true;
354:
355: IGImage existingImage = null;
356:
357: try {
358: IGFolderUtil.findByPrimaryKey(folderId);
359:
360: if (context.getDataStrategy().equals(
361: PortletDataHandlerKeys.DATA_STRATEGY_MIRROR)) {
362:
363: try {
364: existingImage = IGImageFinderUtil.findByUuid_G(
365: igImage.getUuid(), context.getGroupId());
366:
367: IGImageLocalServiceUtil.updateImage(userId,
368: existingImage.getImageId(), folderId,
369: igImage.getDescription(), imageFile,
370: igImage.getImageType(), tagsEntries);
371: } catch (NoSuchImageException nsie) {
372: IGImageLocalServiceUtil.addImage(igImage.getUuid(),
373: userId, folderId, igImage.getDescription(),
374: imageFile, igImage.getImageType(),
375: tagsEntries, addCommunityPermissions,
376: addGuestPermissions);
377: }
378: } else {
379: IGImageLocalServiceUtil.addImage(userId, folderId,
380: igImage.getDescription(), imageFile, igImage
381: .getImageType(), tagsEntries,
382: addCommunityPermissions, addGuestPermissions);
383: }
384: } catch (NoSuchFolderException nsfe) {
385: _log.error("Could not find the parent folder for IG image "
386: + igImage.getImageId());
387: }
388: }
389:
390: private static final String _NAMESPACE = "image_gallery";
391:
392: private static final String _IMAGE_GALLERY_FOLDER = "image-gallery/";
393:
394: private static final PortletDataHandlerBoolean _foldersAndImages = new PortletDataHandlerBoolean(
395: _NAMESPACE, "folders-and-images", true, true);
396:
397: private static final PortletDataHandlerBoolean _tags = new PortletDataHandlerBoolean(
398: _NAMESPACE, "tags");
399:
400: private static Log _log = LogFactory
401: .getLog(IGPortletDataHandlerImpl.class);
402:
403: }
|