1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.verify;
16  
17  import com.liferay.portal.NoSuchCompanyException;
18  import com.liferay.portal.NoSuchLayoutException;
19  import com.liferay.portal.NoSuchUserException;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.model.Image;
23  import com.liferay.portal.service.CompanyLocalServiceUtil;
24  import com.liferay.portal.service.ImageLocalServiceUtil;
25  import com.liferay.portal.service.LayoutLocalServiceUtil;
26  import com.liferay.portal.service.UserLocalServiceUtil;
27  import com.liferay.portlet.imagegallery.NoSuchImageException;
28  import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
29  import com.liferay.portlet.journal.NoSuchArticleImageException;
30  import com.liferay.portlet.journal.NoSuchTemplateException;
31  import com.liferay.portlet.journal.model.JournalArticle;
32  import com.liferay.portlet.journal.service.JournalArticleImageLocalServiceUtil;
33  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
34  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
35  import com.liferay.portlet.shopping.NoSuchItemException;
36  import com.liferay.portlet.shopping.service.ShoppingItemLocalServiceUtil;
37  import com.liferay.portlet.softwarecatalog.NoSuchProductScreenshotException;
38  import com.liferay.portlet.softwarecatalog.service.SCProductScreenshotLocalServiceUtil;
39  
40  import java.util.List;
41  
42  /**
43   * <a href="VerifyImage.java.html"><b><i>View Source</i></b></a>
44   *
45   * <p>
46   * This class is very powerful because it removes all images that it believes is
47   * stale. Do not run this unless you are also not managing images in Liferay's
48   * Image service for your custom models.
49   * </p>
50   *
51   * @author Brian Wing Shun Chan
52   */
53  public class VerifyImage extends VerifyProcess {
54  
55      protected void doVerify() throws Exception {
56          List<Image> images = ImageLocalServiceUtil.getImages();
57  
58          if (_log.isDebugEnabled()) {
59              _log.debug("Processing " + images.size() + " stale images");
60          }
61  
62          for (Image image : images) {
63              if (isStaleImage(image)) {
64                  if (_log.isInfoEnabled()) {
65                      _log.info("Deleting stale image " + image.getImageId());
66                  }
67  
68                  ImageLocalServiceUtil.deleteImage(image.getImageId());
69              }
70          }
71      }
72  
73      protected boolean isStaleImage(Image image) throws Exception {
74          long imageId = image.getImageId();
75  
76          try {
77              CompanyLocalServiceUtil.getCompanyByLogoId(imageId);
78  
79              return false;
80          }
81          catch (NoSuchCompanyException nsce) {
82          }
83  
84          try {
85              LayoutLocalServiceUtil.getLayoutByIconImageId(imageId);
86  
87              return false;
88          }
89          catch (NoSuchLayoutException nsle) {
90          }
91  
92          try {
93              UserLocalServiceUtil.getUserByPortraitId(imageId);
94  
95              return false;
96          }
97          catch (NoSuchUserException nsue) {
98          }
99  
100         try {
101             IGImageLocalServiceUtil.getImageBySmallImageId(imageId);
102 
103             return false;
104         }
105         catch (NoSuchImageException nsie) {
106         }
107 
108         try {
109             IGImageLocalServiceUtil.getImageByLargeImageId(imageId);
110 
111             return false;
112         }
113         catch (NoSuchImageException nsie) {
114         }
115 
116         try {
117             IGImageLocalServiceUtil.getImageByCustom1ImageId(imageId);
118 
119             return false;
120         }
121         catch (NoSuchImageException nsie) {
122         }
123 
124         try {
125             IGImageLocalServiceUtil.getImageByCustom2ImageId(imageId);
126 
127             return false;
128         }
129         catch (NoSuchImageException nsie) {
130         }
131 
132         List<JournalArticle> journalArticles =
133             JournalArticleLocalServiceUtil.getArticlesBySmallImageId(imageId);
134 
135         if (journalArticles.size() > 0) {
136             return false;
137         }
138 
139         try {
140             JournalArticleImageLocalServiceUtil.getArticleImage(imageId);
141 
142             return false;
143         }
144         catch (NoSuchArticleImageException nsaie) {
145         }
146 
147         try {
148             JournalTemplateLocalServiceUtil.getTemplateBySmallImageId(imageId);
149 
150             return false;
151         }
152         catch (NoSuchTemplateException nste) {
153         }
154 
155         try {
156             SCProductScreenshotLocalServiceUtil.
157                 getProductScreenshotByFullImageId(imageId);
158 
159             return false;
160         }
161         catch (NoSuchProductScreenshotException nspse) {
162         }
163 
164         try {
165             SCProductScreenshotLocalServiceUtil.
166                 getProductScreenshotByThumbnailId(imageId);
167 
168             return false;
169         }
170         catch (NoSuchProductScreenshotException nspse) {
171         }
172 
173         try {
174             ShoppingItemLocalServiceUtil.getItemByLargeImageId(imageId);
175 
176             return false;
177         }
178         catch (NoSuchItemException nsie) {
179         }
180 
181         try {
182             ShoppingItemLocalServiceUtil.getItemByMediumImageId(imageId);
183 
184             return false;
185         }
186         catch (NoSuchItemException nsie) {
187         }
188 
189         try {
190             ShoppingItemLocalServiceUtil.getItemBySmallImageId(imageId);
191 
192             return false;
193         }
194         catch (NoSuchItemException nsie) {
195         }
196 
197         return true;
198     }
199 
200     private static Log _log = LogFactoryUtil.getLog(VerifyImage.class);
201 
202 }