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