001: /*
002: * Copyright (C) 2003 Erik Swenson - erik@oreports.com
003: *
004: * This program is free software; you can redistribute it and/or modify it under
005: * the terms of the GNU General Public License as published by the Free Software
006: * Foundation; either version 2 of the License, or (at your option) any later
007: * version.
008: *
009: * This program is distributed in the hope that it will be useful, but WITHOUT
010: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
012: * details.
013: *
014: * You should have received a copy of the GNU General Public License along with
015: * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
016: * Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019:
020: package org.efs.openreports.providers;
021:
022: import org.apache.struts2.ServletActionContext;
023:
024: import org.apache.log4j.Logger;
025: import org.efs.openreports.objects.ORProperty;
026:
027: public class DirectoryProvider {
028: protected static Logger log = Logger
029: .getLogger(DirectoryProvider.class.getName());
030:
031: private String reportDirectory;
032: private String reportImageDirectory;
033: private String reportGenerationDirectory;
034: private String tempDirectory;
035: private String separator = System.getProperty("file.separator");
036:
037: private PropertiesProvider propertiesProvider;
038:
039: public DirectoryProvider(PropertiesProvider propertiesProvider)
040: throws ProviderException {
041: this .propertiesProvider = propertiesProvider;
042: init();
043: }
044:
045: protected void init() throws ProviderException {
046: log.info("Loading BaseDirectory from OR_PROPERTIES table.");
047:
048: String baseDirectory = null;
049:
050: ORProperty property = propertiesProvider
051: .getProperty(ORProperty.BASE_DIRECTORY);
052: if (property != null)
053: baseDirectory = property.getValue();
054:
055: if (baseDirectory == null || baseDirectory.trim().length() < 1) {
056: log
057: .info("BaseDirectory not set in OR_PROPERTIES table. Trying to get path from ServletContext.");
058:
059: try {
060: baseDirectory = ServletActionContext
061: .getServletContext().getRealPath("");
062: baseDirectory = baseDirectory + separator + "reports";
063: } catch (NullPointerException npe) {
064: log.info("ServletActionContext not available.");
065: baseDirectory = ".";
066: }
067: }
068:
069: reportDirectory = baseDirectory;
070: if (!reportDirectory.endsWith(separator))
071: reportDirectory += separator;
072: log.info("Report Directory: " + reportDirectory);
073:
074: reportImageDirectory = reportDirectory + "images" + separator;
075: log.info("Report Image Directory: " + reportImageDirectory);
076:
077: //set temp directory path for report virtualization and image generation
078: property = propertiesProvider
079: .getProperty(ORProperty.TEMP_DIRECTORY);
080: if (property != null && property.getValue() != null
081: && property.getValue().trim().length() > 0) {
082: tempDirectory = property.getValue();
083: if (!tempDirectory.endsWith(separator))
084: tempDirectory += separator;
085: log.info("TempDirectory: " + tempDirectory);
086: }
087:
088: //set report generation directory path for storing generated reports
089: property = propertiesProvider
090: .getProperty(ORProperty.REPORT_GENERATION_DIRECTORY);
091: if (property != null && property.getValue() != null
092: && property.getValue().trim().length() > 0) {
093: reportGenerationDirectory = property.getValue();
094: if (!reportGenerationDirectory.endsWith(separator))
095: reportGenerationDirectory += separator;
096: log.info("ReportGenerationDirectory: "
097: + reportGenerationDirectory);
098: }
099:
100: log.info("Created");
101: }
102:
103: public String getReportDirectory() {
104: return reportDirectory;
105: }
106:
107: public void setReportDirectory(String reportDirectory) {
108: this .reportDirectory = reportDirectory;
109: if (!reportDirectory.endsWith(separator))
110: reportDirectory += separator;
111:
112: reportImageDirectory = reportDirectory + "images" + separator;
113: }
114:
115: public String getReportImageDirectory() {
116: return reportImageDirectory;
117: }
118:
119: public String getTempDirectory() {
120: return tempDirectory;
121: }
122:
123: public void setTempDirectory(String tempDirectory) {
124: this .tempDirectory = tempDirectory;
125: if (!tempDirectory.endsWith(separator))
126: tempDirectory += separator;
127: }
128:
129: public String getReportGenerationDirectory() {
130: return reportGenerationDirectory;
131: }
132:
133: public void setReportGenerationDirectory(
134: String reportGenerationDirectory) {
135: this .reportGenerationDirectory = reportGenerationDirectory;
136: if (!reportGenerationDirectory.endsWith(separator))
137: reportGenerationDirectory += separator;
138: }
139:
140: public void setPropertiesProvider(
141: PropertiesProvider propertiesProvider) {
142: this.propertiesProvider = propertiesProvider;
143: }
144:
145: }
|