1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.verify;
24  
25  import com.liferay.portal.NoSuchCompanyException;
26  import com.liferay.portal.NoSuchLayoutException;
27  import com.liferay.portal.NoSuchUserException;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.model.Image;
31  import com.liferay.portal.service.CompanyLocalServiceUtil;
32  import com.liferay.portal.service.ImageLocalServiceUtil;
33  import com.liferay.portal.service.LayoutLocalServiceUtil;
34  import com.liferay.portal.service.UserLocalServiceUtil;
35  import com.liferay.portlet.imagegallery.NoSuchImageException;
36  import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
37  import com.liferay.portlet.journal.NoSuchArticleImageException;
38  import com.liferay.portlet.journal.NoSuchTemplateException;
39  import com.liferay.portlet.journal.model.JournalArticle;
40  import com.liferay.portlet.journal.service.JournalArticleImageLocalServiceUtil;
41  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
42  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
43  import com.liferay.portlet.shopping.NoSuchItemException;
44  import com.liferay.portlet.shopping.service.ShoppingItemLocalServiceUtil;
45  import com.liferay.portlet.softwarecatalog.NoSuchProductScreenshotException;
46  import com.liferay.portlet.softwarecatalog.service.SCProductScreenshotLocalServiceUtil;
47  
48  import java.util.List;
49  
50  /**
51   * <a href="VerifyImage.java.html"><b><i>View Source</i></b></a>
52   *
53   * <p>
54   * This class is very powerful because it removes all images that it believes
55   * is stale. Do not run this unless you are also not managing images in
56   * Liferay's Image service for your custom models.
57   * </p>
58   *
59   * @author Brian Wing Shun Chan
60   *
61   */
62  public class VerifyImage extends VerifyProcess {
63  
64      public void verify() throws VerifyException {
65          _log.info("Verifying");
66  
67          try {
68              verifyImage();
69          }
70          catch (Exception e) {
71              throw new VerifyException(e);
72          }
73      }
74  
75      protected boolean isStaleImage(Image image) throws Exception {
76          long imageId = image.getImageId();
77  
78          try {
79              CompanyLocalServiceUtil.getCompanyByLogoId(imageId);
80  
81              return false;
82          }
83          catch (NoSuchCompanyException nsce) {
84          }
85  
86          try {
87              LayoutLocalServiceUtil.getLayoutByIconImageId(imageId);
88  
89              return false;
90          }
91          catch (NoSuchLayoutException nsle) {
92          }
93  
94          try {
95              UserLocalServiceUtil.getUserByPortraitId(imageId);
96  
97              return false;
98          }
99          catch (NoSuchUserException nsue) {
100         }
101 
102         try {
103             IGImageLocalServiceUtil.getImageBySmallImageId(imageId);
104 
105             return false;
106         }
107         catch (NoSuchImageException nsie) {
108         }
109 
110         try {
111             IGImageLocalServiceUtil.getImageByLargeImageId(imageId);
112 
113             return false;
114         }
115         catch (NoSuchImageException nsie) {
116         }
117 
118         try {
119             IGImageLocalServiceUtil.getImageByCustom1ImageId(imageId);
120 
121             return false;
122         }
123         catch (NoSuchImageException nsie) {
124         }
125 
126         try {
127             IGImageLocalServiceUtil.getImageByCustom2ImageId(imageId);
128 
129             return false;
130         }
131         catch (NoSuchImageException nsie) {
132         }
133 
134         List<JournalArticle> journalArticles =
135             JournalArticleLocalServiceUtil.getArticlesBySmallImageId(imageId);
136 
137         if (journalArticles.size() > 0) {
138             return false;
139         }
140 
141         try {
142             JournalArticleImageLocalServiceUtil.getArticleImage(imageId);
143 
144             return false;
145         }
146         catch (NoSuchArticleImageException nsaie) {
147         }
148 
149         try {
150             JournalTemplateLocalServiceUtil.getTemplateBySmallImageId(imageId);
151 
152             return false;
153         }
154         catch (NoSuchTemplateException nste) {
155         }
156 
157         try {
158             SCProductScreenshotLocalServiceUtil.
159                 getProductScreenshotByFullImageId(imageId);
160 
161             return false;
162         }
163         catch (NoSuchProductScreenshotException nspse) {
164         }
165 
166         try {
167             SCProductScreenshotLocalServiceUtil.
168                 getProductScreenshotByThumbnailId(imageId);
169 
170             return false;
171         }
172         catch (NoSuchProductScreenshotException nspse) {
173         }
174 
175         try {
176             ShoppingItemLocalServiceUtil.getItemByLargeImageId(imageId);
177 
178             return false;
179         }
180         catch (NoSuchItemException nsie) {
181         }
182 
183         try {
184             ShoppingItemLocalServiceUtil.getItemByMediumImageId(imageId);
185 
186             return false;
187         }
188         catch (NoSuchItemException nsie) {
189         }
190 
191         try {
192             ShoppingItemLocalServiceUtil.getItemBySmallImageId(imageId);
193 
194             return false;
195         }
196         catch (NoSuchItemException nsie) {
197         }
198 
199         return true;
200     }
201 
202     protected void verifyImage() throws Exception {
203         List<Image> images = ImageLocalServiceUtil.getImages();
204 
205         if (_log.isDebugEnabled()) {
206             _log.debug("Processing " + images.size() + " stale images");
207         }
208 
209         for (Image image : images) {
210             if (isStaleImage(image)) {
211                 if (_log.isInfoEnabled()) {
212                     _log.info("Deleting stale image " + image.getImageId());
213                 }
214 
215                 ImageLocalServiceUtil.deleteImage(image.getImageId());
216             }
217         }
218     }
219 
220     private static Log _log = LogFactoryUtil.getLog(VerifyImage.class);
221 
222 }