View Javadoc
1   /***
2    * Copyright (C) 2009 TM-Search Community.
3    *
4    * This file is part of TM-Search Services.
5    *
6    * Foobar is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * Foobar is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package eu.europa.tmsearch.services.business;
21  
22  import java.util.Date;
23  
24  import org.apache.log4j.Logger;
25  
26  import eu.europa.tmsearch.services.Cacheable;
27  import eu.europa.tmsearch.services.business.exception.BusinessServiceException;
28  import eu.europa.tmsearch.services.business.exception.ImageProcessorRuntimeException;
29  import eu.europa.tmsearch.services.business.util.AWTImageProcessorImpl;
30  import eu.europa.tmsearch.services.business.util.ImageProcessor;
31  import eu.europa.tmsearch.services.business.util.ST13Converter;
32  import eu.europa.tmsearch.services.business.util.ST13ConverterDummyImpl;
33  import eu.europa.tmsearch.services.dao.TradeMarkImageDAO;
34  import eu.europa.tmsearch.services.model.TradeMarkImage;
35  import eu.europa.tmsearch.services.resources.exceptions.ResourceNotFoundException;
36  
37  /***
38   * A business service to get a thumbnail image given a TradeMark.
39   * 
40   * 
41   * @see TradeMarkImage
42   * @see TradeMark
43   */
44  public class TradeMarkThumbnailServiceImpl implements TradeMarkThumbnailService, Cacheable {
45  
46      private static Logger log = Logger.getLogger(TradeMarkThumbnailServiceImpl.class);
47  
48      /***
49       * A {@link ImageProcessor} implementation used to generate a thumbnail
50       * image.
51       */
52      private ImageProcessor imageProcessor;
53      /***
54       * A {@link TradeMarkImageDAO} implementation used to fetch a trademark
55       * master image.
56       */
57      private TradeMarkImageDAO tradeMarkImageDAO;
58      /***
59       * A default value for thumbnail image height
60       */
61      private int defaultThumbnailHeight = 0;
62      /***
63       * A default value for thumbnail image width
64       */
65      private int defaultThumbnailWidth = 0;
66      /***
67       * A default value for thumbnail image format
68       */
69      private String defaultThumbnailFormat;
70  
71      /***
72       * A from/to ST13 IDs/specific data store ID converter. By default
73       * instantiates the dummy/transparent converter
74       */
75      private ST13Converter st13Converter = new ST13ConverterDummyImpl();
76  
77      /***
78       * Gets a thumbnail image given a TradeMark by its ApplicationNumber.
79       * 
80       * <p>
81       * Uses a {@link ImageProcessor} to generate the thumbnail. If the injected
82       * ImageProcessor implementation throws a
83       * {@link ImageProcessorRuntimeException} this service will try to generate
84       * the thumbnail use standard Java imaging facilities.
85       * </p>
86       * 
87       * @param tradeMark
88       *            A source trade mark to get the master image from
89       * @return A TradeMarkImage containing the thumbnail
90       * @throws BusinessServiceException
91       * @throws ResourceNotFoundException
92       */
93      public TradeMarkImage getThumbnail(String resourceId) throws BusinessServiceException,
94  	    ResourceNotFoundException {
95  
96  	TradeMarkImage tradeMarkImage = tradeMarkImageDAO.getTradeMarkImageByID(st13Converter
97  		.fromST13ID(resourceId));
98  
99  	TradeMarkImage tradeMarkImageThumb;
100 	try {
101 	    tradeMarkImageThumb = imageProcessor.getThumbnailImage(tradeMarkImage, defaultThumbnailHeight,
102 		    defaultThumbnailWidth);
103 	} catch (ImageProcessorRuntimeException e) {
104 	    if (!imageProcessor.getClass().isInstance(AWTImageProcessorImpl.class)) {
105 		log
106 			.error(imageProcessor.getClass().toString()
107 				+ " image processor not found or properly installed. Rolling back to standard Java image processing...");
108 		this.imageProcessor = new AWTImageProcessorImpl();
109 		tradeMarkImageThumb = imageProcessor.getThumbnailImage(tradeMarkImage,
110 			defaultThumbnailHeight, defaultThumbnailWidth);
111 	    } else
112 		throw new BusinessServiceException(e);
113 	}
114 	return tradeMarkImageThumb;
115     }
116 
117     @Override
118     public Date getLastModified(String tradeMarkId) throws ResourceNotFoundException {
119 	return tradeMarkImageDAO.getLastModified(tradeMarkId);
120     }
121 
122     @Override
123     public Date getExpires(String tradeMarkId) throws ResourceNotFoundException {
124 	return tradeMarkImageDAO.getExpires(tradeMarkId);
125     }
126 
127     public ImageProcessor getImageProcessor() {
128 	return imageProcessor;
129     }
130 
131     public void setImageProcessor(ImageProcessor imageProcessor) {
132 	this.imageProcessor = imageProcessor;
133     }
134 
135     public TradeMarkImageDAO getTradeMarkImageDAO() {
136 	return tradeMarkImageDAO;
137     }
138 
139     public void setTradeMarkImageDAO(TradeMarkImageDAO tradeMarkImageDAO) {
140 	this.tradeMarkImageDAO = tradeMarkImageDAO;
141     }
142 
143     public int getDefaultThumbnailHeight() {
144 	return defaultThumbnailHeight;
145     }
146 
147     public void setDefaultThumbnailHeight(int defaultThumbnailHeight) {
148 	this.defaultThumbnailHeight = defaultThumbnailHeight;
149     }
150 
151     public int getDefaultThumbnailWidth() {
152 	return defaultThumbnailWidth;
153     }
154 
155     public void setDefaultThumbnailWidth(int defaultThumbnailWidth) {
156 	this.defaultThumbnailWidth = defaultThumbnailWidth;
157     }
158 
159     public String getDefaultThumbnailFormat() {
160 	return defaultThumbnailFormat;
161     }
162 
163     public void setDefaultThumbnailFormat(String defaultThumbnailFormat) {
164 	this.defaultThumbnailFormat = defaultThumbnailFormat;
165     }
166 
167     public ST13Converter getSt13Converter() {
168 	return st13Converter;
169     }
170 
171     public void setSt13Converter(ST13Converter st13Converter) {
172 	this.st13Converter = st13Converter;
173     }
174 
175 }