001    /*
002     * Copyright 2011 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     */
016    package org.kuali.kfs.module.purap.dataaccess.impl;
017    
018    import java.text.DecimalFormat;
019    import java.text.NumberFormat;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    import org.kuali.kfs.module.purap.PurapConstants;
025    import org.kuali.kfs.module.purap.dataaccess.ImageDao;
026    import org.kuali.kfs.module.purap.document.ContractManagerAssignmentDocument;
027    import org.kuali.kfs.module.purap.exception.PurapConfigurationException;
028    import org.kuali.kfs.sys.KFSConstants;
029    import org.kuali.kfs.sys.service.impl.KfsParameterConstants;
030    import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
031    import org.kuali.rice.kns.service.KualiConfigurationService;
032    import org.kuali.rice.kns.service.ParameterService;
033    
034    /**
035     * OJB Implementation of ImageDao.
036     */
037    public class ImageDaoNet extends PlatformAwareDaoBaseOjb implements ImageDao {
038        private static Log LOG = LogFactory.getLog(ImageDaoNet.class);
039    
040        private KualiConfigurationService configurationService;
041        private ParameterService parameterService;
042    
043        public void setParameterService(ParameterService parameterService) {
044            this.parameterService = parameterService;
045        }
046    
047        public void setConfigurationService(KualiConfigurationService configurationService) {
048            this.configurationService = configurationService;
049        }
050    
051        /**
052         * @see org.kuali.kfs.module.purap.dataaccess.ImageDao#getPurchasingDirectorImage(java.lang.String, java.lang.String, java.lang.String)
053         */
054        public String getPurchasingDirectorImage(String key, String campusCode, String location) {
055            LOG.debug("getPurchasingDirectorImage() started");
056    
057            String prefix = parameterService.getParameterValue(KfsParameterConstants.PURCHASING_DOCUMENT.class, PurapConstants.PURCHASING_DIRECTOR_IMAGE_PREFIX);
058            String extension = "." + parameterService.getParameterValue(KfsParameterConstants.PURCHASING_DOCUMENT.class, PurapConstants.PURCHASING_DIRECTOR_IMAGE_EXTENSION);
059            return getFile(prefix, campusCode, key, extension, location);
060        }
061    
062        /**
063         * @see org.kuali.kfs.module.purap.dataaccess.ImageDao#getContractManagerImage(java.lang.String, java.lang.Integer, java.lang.String)
064         */
065        public String getContractManagerImage(String key, Integer contractManagerId, String location) {
066            LOG.debug("getContractManagerImage() started");
067    
068            NumberFormat formatter = new DecimalFormat("00");
069            String cm = formatter.format(contractManagerId);
070    
071            String prefix = parameterService.getParameterValue(ContractManagerAssignmentDocument.class, PurapConstants.CONTRACT_MANAGER_IMAGE_PREFIX);
072            String extension = "." + parameterService.getParameterValue(ContractManagerAssignmentDocument.class, PurapConstants.CONTRACT_MANAGER_IMAGE_EXTENSION);
073            return getFile(prefix, cm, key, extension, location);
074        }
075    
076        /**
077         * @see org.kuali.kfs.module.purap.dataaccess.ImageDao#getLogo(java.lang.String, java.lang.String, java.lang.String)
078         */
079        public String getLogo(String key, String campusCode, String location) {
080            LOG.debug("getLogo() started. key is " + key + ". campusCode is " + campusCode);
081    
082            String prefix = parameterService.getParameterValue(KfsParameterConstants.PURCHASING_DOCUMENT.class, PurapConstants.LOGO_IMAGE_PREFIX);
083            String extension = "." + parameterService.getParameterValue(KfsParameterConstants.PURCHASING_DOCUMENT.class, PurapConstants.LOGO_IMAGE_EXTENSION);
084            return getFile(prefix, campusCode, key, extension, location);
085        }
086    
087        /**
088         * Copy a file from a web location to the local system.
089         * 
090         * @param prefix - Prefix for the file name
091         * @param fileKey - File key for file
092         * @param key - Unique key for the file
093         * @param location - location of file
094         * @return - location to copied file
095         */
096        protected String getFile(String prefix, String fileKey, String key, String extension, String location) {
097            LOG.debug("getFile() started");
098    
099            // try retrieving file URL from parameter
100            String urlpath = parameterService.getParameterValue(KfsParameterConstants.PURCHASING_DOCUMENT.class, PurapConstants.PDF_IMAGE_LOCATION_URL);
101            // if parameter value is empty, then try retrieving it from property
102            if (StringUtils.isEmpty(urlpath)) {
103                urlpath = configurationService.getPropertyString(KFSConstants.EXTERNALIZABLE_IMAGES_URL_KEY);
104            }
105            
106            if (urlpath == null) {
107                throw new PurapConfigurationException("Application Setting " + KFSConstants.EXTERNALIZABLE_IMAGES_URL_KEY + " is missing");
108            }
109            if (location == null) {
110                throw new PurapConfigurationException("Valid location to store temp image files was null");
111            }
112    
113            String completeUrl = urlpath + prefix + "_" + fileKey.toLowerCase() + extension;
114            LOG.debug("getFile() URL = " + completeUrl);
115            return completeUrl;
116        }
117    
118    }