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.business.exception.BusinessServiceException; 27 import eu.europa.tmsearch.services.business.exception.ImageProcessorRuntimeException; 28 import eu.europa.tmsearch.services.business.util.AWTImageProcessorImpl; 29 import eu.europa.tmsearch.services.business.util.ImageProcessor; 30 import eu.europa.tmsearch.services.business.util.ST13Converter; 31 import eu.europa.tmsearch.services.business.util.ST13ConverterDummyImpl; 32 import eu.europa.tmsearch.services.dao.TradeMarkImageDAO; 33 import eu.europa.tmsearch.services.model.TradeMarkImage; 34 import eu.europa.tmsearch.services.resources.exceptions.ResourceNotFoundException; 35 36 /*** 37 * A business service to get an image given a TradeMark. 38 * 39 * 40 * @see TradeMarkImage 41 * @see TradeMark 42 */ 43 public class TradeMarkImageServiceImpl implements TradeMarkImageService { 44 45 private static Logger log = Logger.getLogger(TradeMarkImageServiceImpl.class); 46 47 /*** 48 * A from/to ST13 IDs/specific data store ID converter. By default 49 * instantiates the dummy/transparent converter 50 */ 51 private ST13Converter st13Converter = new ST13ConverterDummyImpl(); 52 53 /*** 54 * A {@link ImageProcessor} implementation that MAY BE used to generate the 55 * image if conversion are required. 56 */ 57 private ImageProcessor imageProcessor; 58 /*** 59 * A {@link TradeMarkImageDAO} implementation used to fetch a trademark 60 * master image. 61 */ 62 private TradeMarkImageDAO tradeMarkImageDAO; 63 /*** 64 * A default value for thumbnail image format 65 */ 66 private String defaultThumbnailFormat; 67 68 /*** 69 * Gets an image given a TradeMark by its ApplicationNumber. 70 * 71 * <p> 72 * Uses a {@link ImageProcessor} to generate the image if present and 73 * implementing {@link ImageProcessor#getMasterImage}. If the injected 74 * ImageProcessor implementation throws a 75 * {@link ImageProcessorRuntimeException} this service will try to generate 76 * the image use standard Java imaging facilities. 77 * </p> 78 * 79 * @param tradeMark 80 * A source trade mark to get the master image from 81 * @return A TradeMarkImage containing the master image 82 * @throws BusinessServiceException 83 * @throws ResourceNotFoundException 84 */ 85 public TradeMarkImage getImage(String resourceId) throws BusinessServiceException, 86 ResourceNotFoundException { 87 88 log.debug("Retrieving image from ID: " + resourceId); 89 90 TradeMarkImage tradeMarkImage = tradeMarkImageDAO.getTradeMarkImageByID(st13Converter 91 .fromST13ID(resourceId)); 92 93 TradeMarkImage tradeMarkImageThumb; 94 try { 95 tradeMarkImageThumb = imageProcessor.getImage(tradeMarkImage); 96 } catch (ImageProcessorRuntimeException e) { 97 if (!imageProcessor.getClass().isInstance(AWTImageProcessorImpl.class)) { 98 log 99 .error(imageProcessor.getClass().toString() 100 + " image processor not found or properly installed. Rolling back to standard Java image processing..."); 101 this.imageProcessor = new AWTImageProcessorImpl(); 102 tradeMarkImageThumb = imageProcessor.getImage(tradeMarkImage); 103 } else 104 throw new BusinessServiceException(e); 105 } catch (NullPointerException e) { 106 if (log.isDebugEnabled()) { 107 log 108 .debug("Trade mark's master image has no processor attached. Returning trade mark's master image without processing it."); 109 } 110 tradeMarkImageThumb = tradeMarkImage; 111 } 112 return tradeMarkImageThumb; 113 } 114 115 @Override 116 public Date getLastModified(String tradeMarkID) throws ResourceNotFoundException { 117 String applicationNumber = st13Converter.fromST13ID(tradeMarkID); 118 return tradeMarkImageDAO.getLastModified(applicationNumber); 119 } 120 121 @Override 122 public Date getExpires(String tradeMarkID) throws ResourceNotFoundException { 123 String applicationNumber = st13Converter.fromST13ID(tradeMarkID); 124 return tradeMarkImageDAO.getExpires(applicationNumber); 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 String getDefaultThumbnailFormat() { 144 return defaultThumbnailFormat; 145 } 146 147 public void setDefaultThumbnailFormat(String defaultThumbnailFormat) { 148 this.defaultThumbnailFormat = defaultThumbnailFormat; 149 } 150 151 public ST13Converter getSt13Converter() { 152 return st13Converter; 153 } 154 155 public void setSt13Converter(ST13Converter st13Converter) { 156 this.st13Converter = st13Converter; 157 } 158 159 }