View Javadoc
1   /*
2    * Copyright 2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.module.purap.service.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.kuali.ole.module.purap.PurapConstants;
22  import org.kuali.ole.module.purap.document.ContractManagerAssignmentDocument;
23  import org.kuali.ole.module.purap.exception.PurapConfigurationException;
24  import org.kuali.ole.module.purap.service.ImageService;
25  import org.kuali.ole.sys.OLEConstants;
26  import org.kuali.ole.sys.service.impl.OleParameterConstants;
27  import org.kuali.rice.core.api.config.property.ConfigurationService;
28  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
29  
30  import java.text.DecimalFormat;
31  import java.text.NumberFormat;
32  
33  /**
34   * Implementation of ImageService.
35   */
36  public class ImageServiceImpl implements ImageService {
37      private static Log LOG = LogFactory.getLog(ImageServiceImpl.class);
38  
39      private ConfigurationService configurationService;
40      private ParameterService parameterService;
41  
42      public void setParameterService(ParameterService parameterService) {
43          this.parameterService = parameterService;
44      }
45  
46      public void setConfigurationService(ConfigurationService configurationService) {
47          this.configurationService = configurationService;
48      }
49  
50      /**
51       * @see org.kuali.ole.module.purap.dataaccess.ImageDao#getPurchasingDirectorImage(java.lang.String, java.lang.String, java.lang.String)
52       */
53      public String getPurchasingDirectorImage(String key, String campusCode, String location) {
54          LOG.debug("getPurchasingDirectorImage() started");
55  
56          String prefix = parameterService.getParameterValueAsString(OleParameterConstants.PURCHASING_DOCUMENT.class, PurapConstants.PURCHASING_DIRECTOR_IMAGE_PREFIX);
57          String extension = "." + parameterService.getParameterValueAsString(OleParameterConstants.PURCHASING_DOCUMENT.class, PurapConstants.PURCHASING_DIRECTOR_IMAGE_EXTENSION);
58          return getFile(prefix, campusCode, key, extension, location);
59      }
60  
61      /**
62       * @see org.kuali.ole.module.purap.dataaccess.ImageDao#getContractManagerImage(java.lang.String, java.lang.Integer, java.lang.String)
63       */
64      public String getContractManagerImage(String key, Integer contractManagerId, String location) {
65          LOG.debug("getContractManagerImage() started");
66  
67          NumberFormat formatter = new DecimalFormat("00");
68          String cm = formatter.format(contractManagerId);
69  
70          String prefix = parameterService.getParameterValueAsString(ContractManagerAssignmentDocument.class, PurapConstants.CONTRACT_MANAGER_IMAGE_PREFIX);
71          String extension = "." + parameterService.getParameterValueAsString(ContractManagerAssignmentDocument.class, PurapConstants.CONTRACT_MANAGER_IMAGE_EXTENSION);
72          return getFile(prefix, cm, key, extension, location);
73      }
74  
75      /**
76       * @see org.kuali.ole.module.purap.dataaccess.ImageDao#getLogo(java.lang.String, java.lang.String, java.lang.String)
77       */
78      public String getLogo(String key, String campusCode, String location) {
79          if (LOG.isDebugEnabled()) {
80              LOG.debug("getLogo() started. key is " + key + ". campusCode is " + campusCode);
81          }
82  
83          String prefix = parameterService.getParameterValueAsString(OleParameterConstants.PURCHASING_DOCUMENT.class, PurapConstants.LOGO_IMAGE_PREFIX);
84          String extension = "." + parameterService.getParameterValueAsString(OleParameterConstants.PURCHASING_DOCUMENT.class, PurapConstants.LOGO_IMAGE_EXTENSION);
85          return getFile(prefix, campusCode, key, extension, location);
86      }
87  
88      /**
89       * Copy a file from a web location to the local system.
90       *
91       * @param prefix   - Prefix for the file name
92       * @param fileKey  - File key for file
93       * @param key      - Unique key for the file
94       * @param location - location of file
95       * @return - location to copied file
96       */
97      protected String getFile(String prefix, String fileKey, String key, String extension, String location) {
98          LOG.debug("getFile() started");
99  
100         // try retrieving file URL from parameter
101         String urlpath = parameterService.getParameterValueAsString(OleParameterConstants.PURCHASING_DOCUMENT.class, PurapConstants.PDF_IMAGE_LOCATION_URL);
102         // if parameter value is empty, then try retrieving it from property
103         if (StringUtils.isEmpty(urlpath)) {
104             urlpath = configurationService.getPropertyValueAsString(OLEConstants.EXTERNALIZABLE_IMAGES_URL_KEY);
105         }
106 
107         if (urlpath == null) {
108             throw new PurapConfigurationException("Application Setting " + OLEConstants.EXTERNALIZABLE_IMAGES_URL_KEY + " is missing");
109         }
110         if (location == null) {
111             throw new PurapConfigurationException("Valid location to store temp image files was null");
112         }
113 
114         String completeUrl = urlpath + prefix + "_" + fileKey.toLowerCase() + extension;
115         if (LOG.isDebugEnabled()) {
116             LOG.debug("getFile() URL = " + completeUrl);
117         }
118         return completeUrl;
119     }
120 
121 }