001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.verify;
022:
023: import com.liferay.portal.NoSuchCompanyException;
024: import com.liferay.portal.NoSuchLayoutException;
025: import com.liferay.portal.NoSuchUserException;
026: import com.liferay.portal.model.Image;
027: import com.liferay.portal.service.CompanyLocalServiceUtil;
028: import com.liferay.portal.service.ImageLocalServiceUtil;
029: import com.liferay.portal.service.LayoutLocalServiceUtil;
030: import com.liferay.portal.service.UserLocalServiceUtil;
031: import com.liferay.portlet.imagegallery.NoSuchImageException;
032: import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
033: import com.liferay.portlet.journal.NoSuchArticleImageException;
034: import com.liferay.portlet.journal.NoSuchTemplateException;
035: import com.liferay.portlet.journal.service.JournalArticleImageLocalServiceUtil;
036: import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
037: import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
038: import com.liferay.portlet.shopping.NoSuchItemException;
039: import com.liferay.portlet.shopping.service.ShoppingItemLocalServiceUtil;
040: import com.liferay.portlet.softwarecatalog.NoSuchProductScreenshotException;
041: import com.liferay.portlet.softwarecatalog.service.SCProductScreenshotLocalServiceUtil;
042:
043: import java.util.List;
044:
045: import org.apache.commons.logging.Log;
046: import org.apache.commons.logging.LogFactory;
047:
048: /**
049: * <a href="VerifyImage.java.html"><b><i>View Source</i></b></a>
050: *
051: * <p>
052: * This class is very powerful because it removes all images that it believes
053: * is stale. Do not run this unless you are also not managing images in
054: * Liferay's Image service for your custom models.
055: * </p>
056: *
057: * @author Brian Wing Shun Chan
058: *
059: */
060: public class VerifyImage extends VerifyProcess {
061:
062: public void verify() throws VerifyException {
063: _log.info("Verifying integrity");
064:
065: try {
066: verifyImage();
067: } catch (Exception e) {
068: throw new VerifyException(e);
069: }
070: }
071:
072: protected boolean isStaleImage(Image image) throws Exception {
073: long imageId = image.getImageId();
074:
075: try {
076: CompanyLocalServiceUtil.getCompanyByLogoId(imageId);
077:
078: return false;
079: } catch (NoSuchCompanyException nsce) {
080: }
081:
082: try {
083: LayoutLocalServiceUtil.getLayoutByIconImageId(imageId);
084:
085: return false;
086: } catch (NoSuchLayoutException nsle) {
087: }
088:
089: try {
090: UserLocalServiceUtil.getUserByPortraitId(imageId);
091:
092: return false;
093: } catch (NoSuchUserException nsue) {
094: }
095:
096: try {
097: IGImageLocalServiceUtil.getImageBySmallImageId(imageId);
098:
099: return false;
100: } catch (NoSuchImageException nsie) {
101: }
102:
103: try {
104: IGImageLocalServiceUtil.getImageByLargeImageId(imageId);
105:
106: return false;
107: } catch (NoSuchImageException nsie) {
108: }
109:
110: List journalArticles = JournalArticleLocalServiceUtil
111: .getArticlesBySmallImageId(imageId);
112:
113: if (journalArticles.size() > 0) {
114: return false;
115: }
116:
117: try {
118: JournalArticleImageLocalServiceUtil
119: .getArticleImage(imageId);
120:
121: return false;
122: } catch (NoSuchArticleImageException nsaie) {
123: }
124:
125: try {
126: JournalTemplateLocalServiceUtil
127: .getTemplateBySmallImageId(imageId);
128:
129: return false;
130: } catch (NoSuchTemplateException nste) {
131: }
132:
133: try {
134: SCProductScreenshotLocalServiceUtil
135: .getProductScreenshotByFullImageId(imageId);
136:
137: return false;
138: } catch (NoSuchProductScreenshotException nspse) {
139: }
140:
141: try {
142: SCProductScreenshotLocalServiceUtil
143: .getProductScreenshotByThumbnailId(imageId);
144:
145: return false;
146: } catch (NoSuchProductScreenshotException nspse) {
147: }
148:
149: try {
150: ShoppingItemLocalServiceUtil.getItemByLargeImageId(imageId);
151:
152: return false;
153: } catch (NoSuchItemException nsie) {
154: }
155:
156: try {
157: ShoppingItemLocalServiceUtil
158: .getItemByMediumImageId(imageId);
159:
160: return false;
161: } catch (NoSuchItemException nsie) {
162: }
163:
164: try {
165: ShoppingItemLocalServiceUtil.getItemBySmallImageId(imageId);
166:
167: return false;
168: } catch (NoSuchItemException nsie) {
169: }
170:
171: return true;
172: }
173:
174: protected void verifyImage() throws Exception {
175: List images = ImageLocalServiceUtil.getImages();
176:
177: if (_log.isDebugEnabled()) {
178: _log.debug("Processing " + images.size() + " stale images");
179: }
180:
181: for (int i = 0; i < images.size(); i++) {
182: Image image = (Image) images.get(i);
183:
184: if (isStaleImage(image)) {
185: if (_log.isInfoEnabled()) {
186: _log.info("Deleting stale image "
187: + image.getImageId());
188: }
189:
190: ImageLocalServiceUtil.deleteImage(image.getImageId());
191: }
192: }
193: }
194:
195: private static Log _log = LogFactory.getLog(VerifyImage.class);
196:
197: }
|