1   /**
2    * Copyright (c) 2000-2008 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.model.Image;
29  import com.liferay.portal.service.CompanyLocalServiceUtil;
30  import com.liferay.portal.service.ImageLocalServiceUtil;
31  import com.liferay.portal.service.LayoutLocalServiceUtil;
32  import com.liferay.portal.service.UserLocalServiceUtil;
33  import com.liferay.portlet.imagegallery.NoSuchImageException;
34  import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
35  import com.liferay.portlet.journal.NoSuchArticleImageException;
36  import com.liferay.portlet.journal.NoSuchTemplateException;
37  import com.liferay.portlet.journal.model.JournalArticle;
38  import com.liferay.portlet.journal.service.JournalArticleImageLocalServiceUtil;
39  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
40  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
41  import com.liferay.portlet.shopping.NoSuchItemException;
42  import com.liferay.portlet.shopping.service.ShoppingItemLocalServiceUtil;
43  import com.liferay.portlet.softwarecatalog.NoSuchProductScreenshotException;
44  import com.liferay.portlet.softwarecatalog.service.SCProductScreenshotLocalServiceUtil;
45  
46  import java.util.List;
47  
48  import org.apache.commons.logging.Log;
49  import org.apache.commons.logging.LogFactory;
50  
51  /**
52   * <a href="VerifyImage.java.html"><b><i>View Source</i></b></a>
53   *
54   * <p>
55   * This class is very powerful because it removes all images that it believes
56   * is stale. Do not run this unless you are also not managing images in
57   * Liferay's Image service for your custom models.
58   * </p>
59   *
60   * @author Brian Wing Shun Chan
61   *
62   */
63  public class VerifyImage extends VerifyProcess {
64  
65      public void verify() throws VerifyException {
66          _log.info("Verifying");
67  
68          try {
69              verifyImage();
70          }
71          catch (Exception e) {
72              throw new VerifyException(e);
73          }
74      }
75  
76      protected boolean isStaleImage(Image image) throws Exception {
77          long imageId = image.getImageId();
78  
79          try {
80              CompanyLocalServiceUtil.getCompanyByLogoId(imageId);
81  
82              return false;
83          }
84          catch (NoSuchCompanyException nsce) {
85          }
86  
87          try {
88              LayoutLocalServiceUtil.getLayoutByIconImageId(imageId);
89  
90              return false;
91          }
92          catch (NoSuchLayoutException nsle) {
93          }
94  
95          try {
96              UserLocalServiceUtil.getUserByPortraitId(imageId);
97  
98              return false;
99          }
100         catch (NoSuchUserException nsue) {
101         }
102 
103         try {
104             IGImageLocalServiceUtil.getImageBySmallImageId(imageId);
105 
106             return false;
107         }
108         catch (NoSuchImageException nsie) {
109         }
110 
111         try {
112             IGImageLocalServiceUtil.getImageByLargeImageId(imageId);
113 
114             return false;
115         }
116         catch (NoSuchImageException nsie) {
117         }
118 
119         try {
120             IGImageLocalServiceUtil.getImageByCustom1ImageId(imageId);
121 
122             return false;
123         }
124         catch (NoSuchImageException nsie) {
125         }
126 
127         try {
128             IGImageLocalServiceUtil.getImageByCustom2ImageId(imageId);
129 
130             return false;
131         }
132         catch (NoSuchImageException nsie) {
133         }
134 
135         List<JournalArticle> journalArticles =
136             JournalArticleLocalServiceUtil.getArticlesBySmallImageId(imageId);
137 
138         if (journalArticles.size() > 0) {
139             return false;
140         }
141 
142         try {
143             JournalArticleImageLocalServiceUtil.getArticleImage(imageId);
144 
145             return false;
146         }
147         catch (NoSuchArticleImageException nsaie) {
148         }
149 
150         try {
151             JournalTemplateLocalServiceUtil.getTemplateBySmallImageId(imageId);
152 
153             return false;
154         }
155         catch (NoSuchTemplateException nste) {
156         }
157 
158         try {
159             SCProductScreenshotLocalServiceUtil.
160                 getProductScreenshotByFullImageId(imageId);
161 
162             return false;
163         }
164         catch (NoSuchProductScreenshotException nspse) {
165         }
166 
167         try {
168             SCProductScreenshotLocalServiceUtil.
169                 getProductScreenshotByThumbnailId(imageId);
170 
171             return false;
172         }
173         catch (NoSuchProductScreenshotException nspse) {
174         }
175 
176         try {
177             ShoppingItemLocalServiceUtil.getItemByLargeImageId(imageId);
178 
179             return false;
180         }
181         catch (NoSuchItemException nsie) {
182         }
183 
184         try {
185             ShoppingItemLocalServiceUtil.getItemByMediumImageId(imageId);
186 
187             return false;
188         }
189         catch (NoSuchItemException nsie) {
190         }
191 
192         try {
193             ShoppingItemLocalServiceUtil.getItemBySmallImageId(imageId);
194 
195             return false;
196         }
197         catch (NoSuchItemException nsie) {
198         }
199 
200         return true;
201     }
202 
203     protected void verifyImage() throws Exception {
204         List<Image> images = ImageLocalServiceUtil.getImages();
205 
206         if (_log.isDebugEnabled()) {
207             _log.debug("Processing " + images.size() + " stale images");
208         }
209 
210         for (Image image : images) {
211             if (isStaleImage(image)) {
212                 if (_log.isInfoEnabled()) {
213                     _log.info("Deleting stale image " + image.getImageId());
214                 }
215 
216                 ImageLocalServiceUtil.deleteImage(image.getImageId());
217             }
218         }
219     }
220 
221     private static Log _log = LogFactory.getLog(VerifyImage.class);
222 
223 }