001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.verify;
016    
017    import com.liferay.portal.NoSuchCompanyException;
018    import com.liferay.portal.NoSuchLayoutException;
019    import com.liferay.portal.NoSuchUserException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.model.Image;
023    import com.liferay.portal.service.CompanyLocalServiceUtil;
024    import com.liferay.portal.service.ImageLocalServiceUtil;
025    import com.liferay.portal.service.LayoutLocalServiceUtil;
026    import com.liferay.portal.service.UserLocalServiceUtil;
027    import com.liferay.portlet.imagegallery.NoSuchImageException;
028    import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
029    import com.liferay.portlet.journal.NoSuchArticleImageException;
030    import com.liferay.portlet.journal.NoSuchTemplateException;
031    import com.liferay.portlet.journal.model.JournalArticle;
032    import com.liferay.portlet.journal.service.JournalArticleImageLocalServiceUtil;
033    import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
034    import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
035    import com.liferay.portlet.shopping.NoSuchItemException;
036    import com.liferay.portlet.shopping.service.ShoppingItemLocalServiceUtil;
037    import com.liferay.portlet.softwarecatalog.NoSuchProductScreenshotException;
038    import com.liferay.portlet.softwarecatalog.service.SCProductScreenshotLocalServiceUtil;
039    
040    import java.util.List;
041    
042    /**
043     * <p>
044     * This class is very powerful because it removes all images that it believes is
045     * stale. Do not run this unless you are also not managing images in Liferay's
046     * Image service for your custom models.
047     * </p>
048     *
049     * @author Brian Wing Shun Chan
050     */
051    public class VerifyImage extends VerifyProcess {
052    
053            protected void doVerify() throws Exception {
054                    List<Image> images = ImageLocalServiceUtil.getImages();
055    
056                    if (_log.isDebugEnabled()) {
057                            _log.debug("Processing " + images.size() + " stale images");
058                    }
059    
060                    for (Image image : images) {
061                            if (isStaleImage(image)) {
062                                    if (_log.isInfoEnabled()) {
063                                            _log.info("Deleting stale image " + image.getImageId());
064                                    }
065    
066                                    ImageLocalServiceUtil.deleteImage(image.getImageId());
067                            }
068                    }
069            }
070    
071            protected boolean isStaleImage(Image image) throws Exception {
072                    long imageId = image.getImageId();
073    
074                    try {
075                            CompanyLocalServiceUtil.getCompanyByLogoId(imageId);
076    
077                            return false;
078                    }
079                    catch (NoSuchCompanyException nsce) {
080                    }
081    
082                    try {
083                            LayoutLocalServiceUtil.getLayoutByIconImageId(imageId);
084    
085                            return false;
086                    }
087                    catch (NoSuchLayoutException nsle) {
088                    }
089    
090                    try {
091                            UserLocalServiceUtil.getUserByPortraitId(imageId);
092    
093                            return false;
094                    }
095                    catch (NoSuchUserException nsue) {
096                    }
097    
098                    try {
099                            IGImageLocalServiceUtil.getImageBySmallImageId(imageId);
100    
101                            return false;
102                    }
103                    catch (NoSuchImageException nsie) {
104                    }
105    
106                    try {
107                            IGImageLocalServiceUtil.getImageByLargeImageId(imageId);
108    
109                            return false;
110                    }
111                    catch (NoSuchImageException nsie) {
112                    }
113    
114                    try {
115                            IGImageLocalServiceUtil.getImageByCustom1ImageId(imageId);
116    
117                            return false;
118                    }
119                    catch (NoSuchImageException nsie) {
120                    }
121    
122                    try {
123                            IGImageLocalServiceUtil.getImageByCustom2ImageId(imageId);
124    
125                            return false;
126                    }
127                    catch (NoSuchImageException nsie) {
128                    }
129    
130                    List<JournalArticle> journalArticles =
131                            JournalArticleLocalServiceUtil.getArticlesBySmallImageId(imageId);
132    
133                    if (journalArticles.size() > 0) {
134                            return false;
135                    }
136    
137                    try {
138                            JournalArticleImageLocalServiceUtil.getArticleImage(imageId);
139    
140                            return false;
141                    }
142                    catch (NoSuchArticleImageException nsaie) {
143                    }
144    
145                    try {
146                            JournalTemplateLocalServiceUtil.getTemplateBySmallImageId(imageId);
147    
148                            return false;
149                    }
150                    catch (NoSuchTemplateException nste) {
151                    }
152    
153                    try {
154                            SCProductScreenshotLocalServiceUtil.
155                                    getProductScreenshotByFullImageId(imageId);
156    
157                            return false;
158                    }
159                    catch (NoSuchProductScreenshotException nspse) {
160                    }
161    
162                    try {
163                            SCProductScreenshotLocalServiceUtil.
164                                    getProductScreenshotByThumbnailId(imageId);
165    
166                            return false;
167                    }
168                    catch (NoSuchProductScreenshotException nspse) {
169                    }
170    
171                    try {
172                            ShoppingItemLocalServiceUtil.getItemByLargeImageId(imageId);
173    
174                            return false;
175                    }
176                    catch (NoSuchItemException nsie) {
177                    }
178    
179                    try {
180                            ShoppingItemLocalServiceUtil.getItemByMediumImageId(imageId);
181    
182                            return false;
183                    }
184                    catch (NoSuchItemException nsie) {
185                    }
186    
187                    try {
188                            ShoppingItemLocalServiceUtil.getItemBySmallImageId(imageId);
189    
190                            return false;
191                    }
192                    catch (NoSuchItemException nsie) {
193                    }
194    
195                    return true;
196            }
197    
198            private static Log _log = LogFactoryUtil.getLog(VerifyImage.class);
199    
200    }