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.service.impl;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.util.ByteArrayMaker;
026: import com.liferay.portal.kernel.util.OrderByComparator;
027: import com.liferay.portal.kernel.util.StringPool;
028: import com.liferay.portal.kernel.util.StringUtil;
029: import com.liferay.portal.model.Image;
030: import com.liferay.portal.model.User;
031: import com.liferay.portal.model.impl.ResourceImpl;
032: import com.liferay.portal.service.impl.ImageLocalUtil;
033: import com.liferay.portal.util.PropsUtil;
034: import com.liferay.portal.util.PropsValues;
035: import com.liferay.portlet.imagegallery.ImageNameException;
036: import com.liferay.portlet.imagegallery.ImageSizeException;
037: import com.liferay.portlet.imagegallery.model.IGFolder;
038: import com.liferay.portlet.imagegallery.model.IGImage;
039: import com.liferay.portlet.imagegallery.service.base.IGImageLocalServiceBaseImpl;
040: import com.liferay.portlet.imagegallery.util.Indexer;
041: import com.liferay.util.FileUtil;
042: import com.liferay.util.ImageUtil;
043:
044: import com.sun.media.jai.codec.ImageCodec;
045: import com.sun.media.jai.codec.ImageEncoder;
046:
047: import java.awt.image.RenderedImage;
048:
049: import java.io.File;
050: import java.io.IOException;
051:
052: import java.util.Date;
053: import java.util.Iterator;
054: import java.util.List;
055:
056: import javax.imageio.ImageIO;
057:
058: import org.apache.commons.logging.Log;
059: import org.apache.commons.logging.LogFactory;
060:
061: /**
062: * <a href="IGImageLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
063: *
064: * @author Brian Wing Shun Chan
065: *
066: */
067: public class IGImageLocalServiceImpl extends
068: IGImageLocalServiceBaseImpl {
069:
070: public IGImage addImage(long userId, long folderId,
071: String description, File file, String contentType,
072: String[] tagsEntries, boolean addCommunityPermissions,
073: boolean addGuestPermissions) throws PortalException,
074: SystemException {
075:
076: return addImage(null, userId, folderId, description, file,
077: contentType, tagsEntries, Boolean
078: .valueOf(addCommunityPermissions), Boolean
079: .valueOf(addGuestPermissions), null, null);
080: }
081:
082: public IGImage addImage(String uuid, long userId, long folderId,
083: String description, File file, String contentType,
084: String[] tagsEntries, boolean addCommunityPermissions,
085: boolean addGuestPermissions) throws PortalException,
086: SystemException {
087:
088: return addImage(uuid, userId, folderId, description, file,
089: contentType, tagsEntries, Boolean
090: .valueOf(addCommunityPermissions), Boolean
091: .valueOf(addGuestPermissions), null, null);
092: }
093:
094: public IGImage addImage(long userId, long folderId,
095: String description, File file, String contentType,
096: String[] tagsEntries, String[] communityPermissions,
097: String[] guestPermissions) throws PortalException,
098: SystemException {
099:
100: return addImage(null, userId, folderId, description, file,
101: contentType, tagsEntries, null, null,
102: communityPermissions, guestPermissions);
103: }
104:
105: public IGImage addImage(String uuid, long userId, long folderId,
106: String description, File file, String contentType,
107: String[] tagsEntries, Boolean addCommunityPermissions,
108: Boolean addGuestPermissions, String[] communityPermissions,
109: String[] guestPermissions) throws PortalException,
110: SystemException {
111:
112: try {
113:
114: // Image
115:
116: byte[] bytes = FileUtil.getBytes(file);
117:
118: validate(file, bytes);
119:
120: User user = userPersistence.findByPrimaryKey(userId);
121: IGFolder folder = igFolderPersistence
122: .findByPrimaryKey(folderId);
123: RenderedImage renderedImage = ImageUtil.read(file)
124: .getRenderedImage();
125: Date now = new Date();
126:
127: long imageId = counterLocalService.increment();
128:
129: IGImage image = igImagePersistence.create(imageId);
130:
131: image.setUuid(uuid);
132: image.setCompanyId(user.getCompanyId());
133: image.setUserId(user.getUserId());
134: image.setCreateDate(now);
135: image.setModifiedDate(now);
136: image.setFolderId(folderId);
137: image.setDescription(description);
138: image.setSmallImageId(counterLocalService.increment());
139: image.setLargeImageId(counterLocalService.increment());
140:
141: igImagePersistence.update(image);
142:
143: // Images
144:
145: saveImages(image.getLargeImageId(), renderedImage, image
146: .getSmallImageId(), file, bytes, contentType);
147:
148: // Resources
149:
150: if ((addCommunityPermissions != null)
151: && (addGuestPermissions != null)) {
152:
153: addImageResources(folder, image,
154: addCommunityPermissions.booleanValue(),
155: addGuestPermissions.booleanValue());
156: } else {
157: addImageResources(folder, image, communityPermissions,
158: guestPermissions);
159: }
160:
161: // Tags
162:
163: updateTagsAsset(userId, image, tagsEntries);
164:
165: // Lucene
166:
167: try {
168: Indexer.addImage(image.getCompanyId(), folder
169: .getGroupId(), folderId, imageId, description,
170: tagsEntries);
171: } catch (IOException ioe) {
172: _log.error("Indexing " + imageId, ioe);
173: }
174:
175: return image;
176: } catch (IOException ioe) {
177: throw new ImageSizeException(ioe);
178: }
179: }
180:
181: public void addImageResources(long folderId, long imageId,
182: boolean addCommunityPermissions, boolean addGuestPermissions)
183: throws PortalException, SystemException {
184:
185: IGFolder folder = igFolderPersistence
186: .findByPrimaryKey(folderId);
187: IGImage image = igImagePersistence.findByPrimaryKey(imageId);
188:
189: addImageResources(folder, image, addCommunityPermissions,
190: addGuestPermissions);
191: }
192:
193: public void addImageResources(IGFolder folder, IGImage image,
194: boolean addCommunityPermissions, boolean addGuestPermissions)
195: throws PortalException, SystemException {
196:
197: resourceLocalService.addResources(image.getCompanyId(), folder
198: .getGroupId(), image.getUserId(), IGImage.class
199: .getName(), image.getImageId(), false,
200: addCommunityPermissions, addGuestPermissions);
201: }
202:
203: public void addImageResources(long folderId, long imageId,
204: String[] communityPermissions, String[] guestPermissions)
205: throws PortalException, SystemException {
206:
207: IGFolder folder = igFolderPersistence
208: .findByPrimaryKey(folderId);
209: IGImage image = igImagePersistence.findByPrimaryKey(imageId);
210:
211: addImageResources(folder, image, communityPermissions,
212: guestPermissions);
213: }
214:
215: public void addImageResources(IGFolder folder, IGImage image,
216: String[] communityPermissions, String[] guestPermissions)
217: throws PortalException, SystemException {
218:
219: resourceLocalService.addModelResources(image.getCompanyId(),
220: folder.getGroupId(), image.getUserId(), IGImage.class
221: .getName(), image.getImageId(),
222: communityPermissions, guestPermissions);
223: }
224:
225: public void deleteImage(long imageId) throws PortalException,
226: SystemException {
227:
228: IGImage image = igImagePersistence.findByPrimaryKey(imageId);
229:
230: deleteImage(image);
231: }
232:
233: public void deleteImage(IGImage image) throws PortalException,
234: SystemException {
235:
236: // Lucene
237:
238: try {
239: Indexer.deleteImage(image.getCompanyId(), image
240: .getImageId());
241: } catch (IOException ioe) {
242: _log.error("Deleting index " + image.getImageId(), ioe);
243: }
244:
245: // Tags
246:
247: tagsAssetLocalService.deleteAsset(IGImage.class.getName(),
248: image.getImageId());
249:
250: // Resources
251:
252: resourceLocalService.deleteResource(image.getCompanyId(),
253: IGImage.class.getName(), ResourceImpl.SCOPE_INDIVIDUAL,
254: image.getImageId());
255:
256: // Images
257:
258: ImageLocalUtil.deleteImage(image.getSmallImageId());
259: ImageLocalUtil.deleteImage(image.getLargeImageId());
260:
261: // Image
262:
263: igImagePersistence.remove(image.getPrimaryKey());
264: }
265:
266: public void deleteImages(long folderId) throws PortalException,
267: SystemException {
268:
269: Iterator itr = igImagePersistence.findByFolderId(folderId)
270: .iterator();
271:
272: while (itr.hasNext()) {
273: IGImage image = (IGImage) itr.next();
274:
275: deleteImage(image);
276: }
277: }
278:
279: public int getFoldersImagesCount(List folderIds)
280: throws SystemException {
281:
282: return igImageFinder.countByFolderIds(folderIds);
283: }
284:
285: public List getGroupImages(long groupId, int begin, int end)
286: throws SystemException {
287:
288: return igImageFinder.findByGroupId(groupId, begin, end);
289: }
290:
291: public List getGroupImages(long groupId, long userId, int begin,
292: int end) throws SystemException {
293:
294: if (userId <= 0) {
295: return igImageFinder.findByGroupId(groupId, begin, end);
296: } else {
297: return igImageFinder.findByG_U(groupId, userId, begin, end);
298: }
299: }
300:
301: public int getGroupImagesCount(long groupId) throws SystemException {
302: return igImageFinder.countByGroupId(groupId);
303: }
304:
305: public int getGroupImagesCount(long groupId, long userId)
306: throws SystemException {
307:
308: if (userId <= 0) {
309: return igImageFinder.countByGroupId(groupId);
310: } else {
311: return igImageFinder.countByG_U(groupId, userId);
312: }
313: }
314:
315: public IGImage getImage(long imageId) throws PortalException,
316: SystemException {
317:
318: return igImagePersistence.findByPrimaryKey(imageId);
319: }
320:
321: public IGImage getImageByLargeImageId(long largeImageId)
322: throws PortalException, SystemException {
323:
324: return igImagePersistence.findByLargeImageId(largeImageId);
325: }
326:
327: public IGImage getImageBySmallImageId(long smallImageId)
328: throws PortalException, SystemException {
329:
330: return igImagePersistence.findBySmallImageId(smallImageId);
331: }
332:
333: public IGImage getImageByUuidAndGroupId(String uuid, long groupId)
334: throws PortalException, SystemException {
335:
336: return igImageFinder.findByUuid_G(uuid, groupId);
337: }
338:
339: public List getImages(long folderId) throws SystemException {
340: return igImagePersistence.findByFolderId(folderId);
341: }
342:
343: public List getImages(long folderId, int begin, int end)
344: throws SystemException {
345:
346: return igImagePersistence.findByFolderId(folderId, begin, end);
347: }
348:
349: public List getImages(long folderId, int begin, int end,
350: OrderByComparator obc) throws SystemException {
351:
352: return igImagePersistence.findByFolderId(folderId, begin, end,
353: obc);
354: }
355:
356: public int getImagesCount(long folderId) throws SystemException {
357: return igImagePersistence.countByFolderId(folderId);
358: }
359:
360: public List getNoAssetImages() throws SystemException {
361: return igImageFinder.findByNoAssets();
362: }
363:
364: public IGImage updateImage(long userId, long imageId,
365: long folderId, String description, File file,
366: String contentType, String[] tagsEntries)
367: throws PortalException, SystemException {
368:
369: try {
370:
371: // Image
372:
373: IGImage image = igImagePersistence
374: .findByPrimaryKey(imageId);
375:
376: IGFolder folder = getFolder(image, folderId);
377:
378: RenderedImage renderedImage = null;
379: byte[] bytes = null;
380:
381: if (file != null) {
382: if (file.exists()) {
383: renderedImage = ImageUtil.read(file)
384: .getRenderedImage();
385: bytes = FileUtil.getBytes(file);
386: }
387:
388: validate(file, bytes);
389: }
390:
391: image.setModifiedDate(new Date());
392: image.setFolderId(folder.getFolderId());
393: image.setDescription(description);
394:
395: igImagePersistence.update(image);
396:
397: // Images
398:
399: if (renderedImage != null) {
400: saveImages(image.getLargeImageId(), renderedImage,
401: image.getSmallImageId(), file, bytes,
402: contentType);
403: }
404:
405: // Tags
406:
407: updateTagsAsset(userId, image, tagsEntries);
408:
409: // Lucene
410:
411: try {
412: Indexer.updateImage(image.getCompanyId(), folder
413: .getGroupId(), folder.getFolderId(), imageId,
414: description, tagsEntries);
415: } catch (IOException ioe) {
416: _log.error("Indexing " + imageId, ioe);
417: }
418:
419: return image;
420: } catch (IOException ioe) {
421: throw new ImageSizeException(ioe);
422: }
423: }
424:
425: public void updateTagsAsset(long userId, IGImage image,
426: String[] tagsEntries) throws PortalException,
427: SystemException {
428:
429: Image largeImage = ImageLocalUtil.getImage(image
430: .getLargeImageId());
431:
432: if (largeImage == null) {
433: return;
434: }
435:
436: tagsAssetLocalService.updateAsset(userId, image.getFolder()
437: .getGroupId(), IGImage.class.getName(), image
438: .getImageId(), tagsEntries, null, null, null, null,
439: largeImage.getType(), null, image.getDescription(),
440: null, null, largeImage.getHeight(), largeImage
441: .getWidth(), null, false);
442: }
443:
444: protected IGFolder getFolder(IGImage image, long folderId)
445: throws PortalException, SystemException {
446:
447: if (image.getFolderId() != folderId) {
448: IGFolder oldFolder = igFolderPersistence
449: .findByPrimaryKey(image.getFolderId());
450:
451: IGFolder newFolder = igFolderPersistence
452: .fetchByPrimaryKey(folderId);
453:
454: if ((newFolder == null)
455: || (oldFolder.getGroupId() != newFolder
456: .getGroupId())) {
457:
458: folderId = image.getFolderId();
459: }
460: }
461:
462: return igFolderPersistence.findByPrimaryKey(folderId);
463: }
464:
465: protected void saveImages(long largeImageId,
466: RenderedImage renderedImage, long smallImageId, File file,
467: byte[] bytes, String contentType) throws SystemException {
468:
469: try {
470:
471: // Image
472:
473: ImageLocalUtil.updateImage(largeImageId, bytes);
474:
475: // Thumbnail
476:
477: RenderedImage thumbnail = ImageUtil.scale(renderedImage,
478: PropsValues.IG_IMAGE_THUMBNAIL_MAX_HEIGHT,
479: PropsValues.IG_IMAGE_THUMBNAIL_MAX_WIDTH);
480:
481: ByteArrayMaker bam = new ByteArrayMaker();
482:
483: if (contentType.indexOf("bmp") != -1) {
484: ImageEncoder encoder = ImageCodec.createImageEncoder(
485: "BMP", bam, null);
486:
487: encoder.encode(thumbnail);
488: } else if (contentType.indexOf("gif") != -1) {
489: ImageUtil.encodeGIF(thumbnail, bam);
490: } else if (contentType.indexOf("jpg") != -1
491: || contentType.indexOf("jpeg") != -1) {
492:
493: ImageIO.write(thumbnail, "jpeg", bam);
494: } else if (contentType.indexOf("png") != -1) {
495: ImageIO.write(thumbnail, "png", bam);
496: } else if (contentType.indexOf("tif") != -1) {
497: ImageEncoder encoder = ImageCodec.createImageEncoder(
498: "TIFF", bam, null);
499:
500: encoder.encode(thumbnail);
501: }
502:
503: ImageLocalUtil.updateImage(smallImageId, bam.toByteArray());
504: } catch (IOException ioe) {
505: throw new SystemException(ioe);
506: }
507: }
508:
509: protected void validate(File file, byte[] bytes)
510: throws PortalException, SystemException {
511:
512: String imageName = StringPool.BLANK;
513:
514: if (file != null) {
515: imageName = file.getName();
516: }
517:
518: boolean validImageExtension = false;
519:
520: String[] imageExtensions = PropsUtil
521: .getArray(PropsUtil.IG_IMAGE_EXTENSIONS);
522:
523: for (int i = 0; i < imageExtensions.length; i++) {
524: if (StringPool.STAR.equals(imageExtensions[i])
525: || StringUtil.endsWith(imageName,
526: imageExtensions[i])) {
527:
528: validImageExtension = true;
529:
530: break;
531: }
532: }
533:
534: if (!validImageExtension) {
535: throw new ImageNameException(imageName);
536: }
537:
538: if ((PropsValues.IG_IMAGE_MAX_SIZE > 0)
539: && ((bytes == null) || (bytes.length > PropsValues.IG_IMAGE_MAX_SIZE))) {
540:
541: throw new ImageSizeException();
542: }
543: }
544:
545: private static Log _log = LogFactory
546: .getLog(IGImageLocalServiceImpl.class);
547:
548: }
|