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.service.JournalArticleImageLocalServiceUtil;
38  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
39  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
40  import com.liferay.portlet.shopping.NoSuchItemException;
41  import com.liferay.portlet.shopping.service.ShoppingItemLocalServiceUtil;
42  import com.liferay.portlet.softwarecatalog.NoSuchProductScreenshotException;
43  import com.liferay.portlet.softwarecatalog.service.SCProductScreenshotLocalServiceUtil;
44  
45  import java.util.List;
46  
47  import org.apache.commons.logging.Log;
48  import org.apache.commons.logging.LogFactory;
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 integrity");
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         List journalArticles =
119             JournalArticleLocalServiceUtil.getArticlesBySmallImageId(imageId);
120 
121         if (journalArticles.size() > 0) {
122             return false;
123         }
124 
125         try {
126             JournalArticleImageLocalServiceUtil.getArticleImage(imageId);
127 
128             return false;
129         }
130         catch (NoSuchArticleImageException nsaie) {
131         }
132 
133         try {
134             JournalTemplateLocalServiceUtil.getTemplateBySmallImageId(imageId);
135 
136             return false;
137         }
138         catch (NoSuchTemplateException nste) {
139         }
140 
141         try {
142             SCProductScreenshotLocalServiceUtil.
143                 getProductScreenshotByFullImageId(imageId);
144 
145             return false;
146         }
147         catch (NoSuchProductScreenshotException nspse) {
148         }
149 
150         try {
151             SCProductScreenshotLocalServiceUtil.
152                 getProductScreenshotByThumbnailId(imageId);
153 
154             return false;
155         }
156         catch (NoSuchProductScreenshotException nspse) {
157         }
158 
159         try {
160             ShoppingItemLocalServiceUtil.getItemByLargeImageId(imageId);
161 
162             return false;
163         }
164         catch (NoSuchItemException nsie) {
165         }
166 
167         try {
168             ShoppingItemLocalServiceUtil.getItemByMediumImageId(imageId);
169 
170             return false;
171         }
172         catch (NoSuchItemException nsie) {
173         }
174 
175         try {
176             ShoppingItemLocalServiceUtil.getItemBySmallImageId(imageId);
177 
178             return false;
179         }
180         catch (NoSuchItemException nsie) {
181         }
182 
183         return true;
184     }
185 
186     protected void verifyImage() throws Exception {
187         List images = ImageLocalServiceUtil.getImages();
188 
189         if (_log.isDebugEnabled()) {
190             _log.debug("Processing " + images.size() + " stale images");
191         }
192 
193         for (int i = 0; i < images.size(); i++) {
194             Image image = (Image)images.get(i);
195 
196             if (isStaleImage(image)) {
197                 if (_log.isInfoEnabled()) {
198                     _log.info("Deleting stale image " + image.getImageId());
199                 }
200 
201                 ImageLocalServiceUtil.deleteImage(image.getImageId());
202             }
203         }
204     }
205 
206     private static Log _log = LogFactory.getLog(VerifyImage.class);
207 
208 }