001/*
002 * Copyright 2007 The Kuali Foundation
003 * 
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 * http://www.opensource.org/licenses/ecl2.php
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.ole.module.purap.service.impl;
017
018import org.apache.commons.lang.StringUtils;
019import org.apache.commons.logging.Log;
020import org.apache.commons.logging.LogFactory;
021import org.kuali.ole.module.purap.PurapConstants;
022import org.kuali.ole.module.purap.document.ContractManagerAssignmentDocument;
023import org.kuali.ole.module.purap.exception.PurapConfigurationException;
024import org.kuali.ole.module.purap.service.ImageService;
025import org.kuali.ole.sys.OLEConstants;
026import org.kuali.ole.sys.service.impl.OleParameterConstants;
027import org.kuali.rice.core.api.config.property.ConfigurationService;
028import org.kuali.rice.coreservice.framework.parameter.ParameterService;
029
030import java.text.DecimalFormat;
031import java.text.NumberFormat;
032
033/**
034 * Implementation of ImageService.
035 */
036public class ImageServiceImpl implements ImageService {
037    private static Log LOG = LogFactory.getLog(ImageServiceImpl.class);
038
039    private ConfigurationService configurationService;
040    private ParameterService parameterService;
041
042    public void setParameterService(ParameterService parameterService) {
043        this.parameterService = parameterService;
044    }
045
046    public void setConfigurationService(ConfigurationService configurationService) {
047        this.configurationService = configurationService;
048    }
049
050    /**
051     * @see org.kuali.ole.module.purap.dataaccess.ImageDao#getPurchasingDirectorImage(java.lang.String, java.lang.String, java.lang.String)
052     */
053    public String getPurchasingDirectorImage(String key, String campusCode, String location) {
054        LOG.debug("getPurchasingDirectorImage() started");
055
056        String prefix = parameterService.getParameterValueAsString(OleParameterConstants.PURCHASING_DOCUMENT.class, PurapConstants.PURCHASING_DIRECTOR_IMAGE_PREFIX);
057        String extension = "." + parameterService.getParameterValueAsString(OleParameterConstants.PURCHASING_DOCUMENT.class, PurapConstants.PURCHASING_DIRECTOR_IMAGE_EXTENSION);
058        return getFile(prefix, campusCode, key, extension, location);
059    }
060
061    /**
062     * @see org.kuali.ole.module.purap.dataaccess.ImageDao#getContractManagerImage(java.lang.String, java.lang.Integer, java.lang.String)
063     */
064    public String getContractManagerImage(String key, Integer contractManagerId, String location) {
065        LOG.debug("getContractManagerImage() started");
066
067        NumberFormat formatter = new DecimalFormat("00");
068        String cm = formatter.format(contractManagerId);
069
070        String prefix = parameterService.getParameterValueAsString(ContractManagerAssignmentDocument.class, PurapConstants.CONTRACT_MANAGER_IMAGE_PREFIX);
071        String extension = "." + parameterService.getParameterValueAsString(ContractManagerAssignmentDocument.class, PurapConstants.CONTRACT_MANAGER_IMAGE_EXTENSION);
072        return getFile(prefix, cm, key, extension, location);
073    }
074
075    /**
076     * @see org.kuali.ole.module.purap.dataaccess.ImageDao#getLogo(java.lang.String, java.lang.String, java.lang.String)
077     */
078    public String getLogo(String key, String campusCode, String location) {
079        if (LOG.isDebugEnabled()) {
080            LOG.debug("getLogo() started. key is " + key + ". campusCode is " + campusCode);
081        }
082
083        String prefix = parameterService.getParameterValueAsString(OleParameterConstants.PURCHASING_DOCUMENT.class, PurapConstants.LOGO_IMAGE_PREFIX);
084        String extension = "." + parameterService.getParameterValueAsString(OleParameterConstants.PURCHASING_DOCUMENT.class, PurapConstants.LOGO_IMAGE_EXTENSION);
085        return getFile(prefix, campusCode, key, extension, location);
086    }
087
088    /**
089     * Copy a file from a web location to the local system.
090     *
091     * @param prefix   - Prefix for the file name
092     * @param fileKey  - File key for file
093     * @param key      - Unique key for the file
094     * @param location - location of file
095     * @return - location to copied file
096     */
097    protected String getFile(String prefix, String fileKey, String key, String extension, String location) {
098        LOG.debug("getFile() started");
099
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}