1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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 is
55   * stale. Do not run this unless you are also not managing images in Liferay's
56   * Image service for your custom models.
57   * </p>
58   *
59   * @author Brian Wing Shun Chan
60   */
61  public class VerifyImage extends VerifyProcess {
62  
63      protected void doVerify() throws Exception {
64          List<Image> images = ImageLocalServiceUtil.getImages();
65  
66          if (_log.isDebugEnabled()) {
67              _log.debug("Processing " + images.size() + " stale images");
68          }
69  
70          for (Image image : images) {
71              if (isStaleImage(image)) {
72                  if (_log.isInfoEnabled()) {
73                      _log.info("Deleting stale image " + image.getImageId());
74                  }
75  
76                  ImageLocalServiceUtil.deleteImage(image.getImageId());
77              }
78          }
79      }
80  
81      protected boolean isStaleImage(Image image) throws Exception {
82          long imageId = image.getImageId();
83  
84          try {
85              CompanyLocalServiceUtil.getCompanyByLogoId(imageId);
86  
87              return false;
88          }
89          catch (NoSuchCompanyException nsce) {
90          }
91  
92          try {
93              LayoutLocalServiceUtil.getLayoutByIconImageId(imageId);
94  
95              return false;
96          }
97          catch (NoSuchLayoutException nsle) {
98          }
99  
100         try {
101             UserLocalServiceUtil.getUserByPortraitId(imageId);
102 
103             return false;
104         }
105         catch (NoSuchUserException nsue) {
106         }
107 
108         try {
109             IGImageLocalServiceUtil.getImageBySmallImageId(imageId);
110 
111             return false;
112         }
113         catch (NoSuchImageException nsie) {
114         }
115 
116         try {
117             IGImageLocalServiceUtil.getImageByLargeImageId(imageId);
118 
119             return false;
120         }
121         catch (NoSuchImageException nsie) {
122         }
123 
124         try {
125             IGImageLocalServiceUtil.getImageByCustom1ImageId(imageId);
126 
127             return false;
128         }
129         catch (NoSuchImageException nsie) {
130         }
131 
132         try {
133             IGImageLocalServiceUtil.getImageByCustom2ImageId(imageId);
134 
135             return false;
136         }
137         catch (NoSuchImageException nsie) {
138         }
139 
140         List<JournalArticle> journalArticles =
141             JournalArticleLocalServiceUtil.getArticlesBySmallImageId(imageId);
142 
143         if (journalArticles.size() > 0) {
144             return false;
145         }
146 
147         try {
148             JournalArticleImageLocalServiceUtil.getArticleImage(imageId);
149 
150             return false;
151         }
152         catch (NoSuchArticleImageException nsaie) {
153         }
154 
155         try {
156             JournalTemplateLocalServiceUtil.getTemplateBySmallImageId(imageId);
157 
158             return false;
159         }
160         catch (NoSuchTemplateException nste) {
161         }
162 
163         try {
164             SCProductScreenshotLocalServiceUtil.
165                 getProductScreenshotByFullImageId(imageId);
166 
167             return false;
168         }
169         catch (NoSuchProductScreenshotException nspse) {
170         }
171 
172         try {
173             SCProductScreenshotLocalServiceUtil.
174                 getProductScreenshotByThumbnailId(imageId);
175 
176             return false;
177         }
178         catch (NoSuchProductScreenshotException nspse) {
179         }
180 
181         try {
182             ShoppingItemLocalServiceUtil.getItemByLargeImageId(imageId);
183 
184             return false;
185         }
186         catch (NoSuchItemException nsie) {
187         }
188 
189         try {
190             ShoppingItemLocalServiceUtil.getItemByMediumImageId(imageId);
191 
192             return false;
193         }
194         catch (NoSuchItemException nsie) {
195         }
196 
197         try {
198             ShoppingItemLocalServiceUtil.getItemBySmallImageId(imageId);
199 
200             return false;
201         }
202         catch (NoSuchItemException nsie) {
203         }
204 
205         return true;
206     }
207 
208     private static Log _log = LogFactoryUtil.getLog(VerifyImage.class);
209 
210 }