001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.module.purap.dao.ojb;
017:
018: import java.io.BufferedInputStream;
019: import java.io.BufferedOutputStream;
020: import java.io.File;
021: import java.io.FileNotFoundException;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.OutputStream;
026: import java.net.MalformedURLException;
027: import java.net.URL;
028: import java.text.DecimalFormat;
029: import java.text.NumberFormat;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.kuali.core.dao.ojb.PlatformAwareDaoBaseOjb;
034: import org.kuali.core.service.KualiConfigurationService;
035: import org.kuali.kfs.KFSConstants;
036: import org.kuali.kfs.service.ParameterService;
037: import org.kuali.kfs.service.impl.ParameterConstants;
038: import org.kuali.module.purap.PurapConstants;
039: import org.kuali.module.purap.dao.ImageDao;
040: import org.kuali.module.purap.document.AssignContractManagerDocument;
041: import org.kuali.module.purap.exceptions.PurError;
042: import org.kuali.module.purap.exceptions.PurapConfigurationException;
043:
044: /**
045: * OJB Implementation of ImageDao.
046: */
047: public class ImageDaoNet extends PlatformAwareDaoBaseOjb implements
048: ImageDao {
049: private static Log LOG = LogFactory.getLog(ImageDaoNet.class);
050:
051: private KualiConfigurationService configurationService;
052: private ParameterService parameterService;
053:
054: public void setParameterService(ParameterService parameterService) {
055: this .parameterService = parameterService;
056: }
057:
058: public void setConfigurationService(
059: KualiConfigurationService configurationService) {
060: this .configurationService = configurationService;
061: }
062:
063: /**
064: * @see org.kuali.module.purap.dao.ImageDao#getPurchasingDirectorImage(java.lang.String, java.lang.String, java.lang.String)
065: */
066: public String getPurchasingDirectorImage(String key,
067: String campusCode, String location) {
068: LOG.debug("getPurchasingDirectorImage() started");
069:
070: String prefix = parameterService.getParameterValue(
071: ParameterConstants.PURCHASING_DOCUMENT.class,
072: PurapConstants.PURCHASING_DIRECTOR_IMAGE_PREFIX);
073: String extension = "."
074: + parameterService
075: .getParameterValue(
076: ParameterConstants.PURCHASING_DOCUMENT.class,
077: PurapConstants.PURCHASING_DIRECTOR_IMAGE_EXTENSION);
078: return getFile(prefix, campusCode, key, extension, location);
079: }
080:
081: /**
082: * @see org.kuali.module.purap.dao.ImageDao#getContractManagerImage(java.lang.String, java.lang.Integer, java.lang.String)
083: */
084: public String getContractManagerImage(String key,
085: Integer contractManagerId, String location) {
086: LOG.debug("getContractManagerImage() started");
087:
088: NumberFormat formatter = new DecimalFormat("00");
089: String cm = formatter.format(contractManagerId);
090:
091: String prefix = parameterService.getParameterValue(
092: AssignContractManagerDocument.class,
093: PurapConstants.CONTRACT_MANAGER_IMAGE_PREFIX);
094: String extension = "."
095: + parameterService
096: .getParameterValue(
097: AssignContractManagerDocument.class,
098: PurapConstants.CONTRACT_MANAGER_IMAGE_EXTENSION);
099: return getFile(prefix, cm, key, extension, location);
100: }
101:
102: /**
103: * @see org.kuali.module.purap.dao.ImageDao#getLogo(java.lang.String, java.lang.String, java.lang.String)
104: */
105: public String getLogo(String key, String campusCode, String location) {
106: LOG.debug("getLogo() started. key is " + key
107: + ". campusCode is " + campusCode);
108:
109: String prefix = parameterService.getParameterValue(
110: ParameterConstants.PURCHASING_DOCUMENT.class,
111: PurapConstants.LOGO_IMAGE_PREFIX);
112: String extension = "."
113: + parameterService.getParameterValue(
114: ParameterConstants.PURCHASING_DOCUMENT.class,
115: PurapConstants.LOGO_IMAGE_EXTENSION);
116: return getFile(prefix, campusCode, key, extension, location);
117: }
118:
119: /**
120: * Copy a file from a web location to the local system.
121: *
122: * @param prefix - Prefix for the file name
123: * @param fileKey - File key for file
124: * @param key - Unique key for the file
125: * @param location - location of file
126: * @return - location to copied file
127: */
128: private String getFile(String prefix, String fileKey, String key,
129: String extension, String location) {
130: LOG.debug("getFile() started");
131:
132: String externalizableUrlSettingName = "externalizable.images.url";
133: String externalizableUrl = configurationService
134: .getPropertyString(KFSConstants.EXTERNALIZABLE_IMAGES_URL_KEY);
135: if (externalizableUrl == null) {
136: throw new PurapConfigurationException(
137: "Application Setting "
138: + externalizableUrlSettingName
139: + " is missing");
140: }
141: if (location == null) {
142: throw new PurapConfigurationException(
143: "Valid location to store temp image files was null");
144: }
145:
146: String completeUrl = externalizableUrl + prefix + "_"
147: + fileKey.toLowerCase() + extension;
148: String completeFile = location + key.toLowerCase() + prefix
149: + extension;
150:
151: LOG.debug("getFile() URL = " + completeUrl);
152: LOG.debug("getFile() File = " + completeFile);
153:
154: OutputStream out = null;
155: BufferedOutputStream bufOut = null;
156: InputStream in = null;
157: BufferedInputStream bufIn = null;
158:
159: try {
160: out = new FileOutputStream(completeFile);
161: bufOut = new BufferedOutputStream(out);
162:
163: URL url = new URL(completeUrl);
164: in = url.openStream();
165: bufIn = new BufferedInputStream(in);
166:
167: // Repeat until end of file
168: while (true) {
169: int data = bufIn.read();
170:
171: // Check for EOF
172: if (data == -1) {
173: break;
174: } else {
175: bufOut.write(data);
176: }
177: }
178: } catch (FileNotFoundException fnfe) {
179: LOG.error("getFile() File not found: " + completeUrl, fnfe);
180: throw new PurapConfigurationException(
181: "getFile() File not found: " + completeUrl);
182: } catch (MalformedURLException mue) {
183: LOG.error("getFile() Unable to get URL: " + completeUrl,
184: mue);
185: throw new IllegalAccessError(mue.getMessage());
186: } catch (IOException ioe) {
187: LOG.error(
188: "getFile() Unable to write file: " + completeFile,
189: ioe);
190: throw new IllegalAccessError(ioe.getMessage());
191: } finally {
192: if (bufIn != null) {
193: try {
194: bufIn.close();
195: } catch (IOException e) {
196: }
197: }
198: if (in != null) {
199: try {
200: in.close();
201: } catch (IOException e) {
202: }
203: }
204: if (bufOut != null) {
205: try {
206: bufOut.close();
207: } catch (IOException e) {
208: }
209: }
210: if (out != null) {
211: try {
212: out.close();
213: } catch (IOException e) {
214: }
215: }
216: }
217: return completeFile;
218: }
219:
220: /**
221: * @see org.kuali.module.purap.dao.ImageDao#removeImages(java.lang.String, java.lang.String)
222: */
223: public void removeImages(String key, String location) {
224: LOG.debug("removeImages() started");
225: try {
226: File dir = new File(location);
227: String[] files = dir.list();
228: for (int i = 0; i < files.length; i++) {
229: String filename = files[i];
230: if (filename.startsWith(key.toLowerCase())) {
231: LOG.debug("removeImages() removing " + filename);
232:
233: File f = new File(location + filename);
234: f.delete();
235: }
236: }
237: } catch (Exception e) {
238: throw new PurError(
239: "Caught exception while trying to remove images at "
240: + location, e);
241: }
242: }
243:
244: }
|