Java tutorial
/* * Copyright (C) 2012 Marius Giepz * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ package org.saiku.adhoc.providers.impl.standalone; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.URI; import java.net.URL; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.io.FilenameUtils; import org.apache.commons.vfs.FileObject; import org.apache.commons.vfs.FileSystemManager; import org.apache.commons.vfs.VFS; import org.pentaho.reporting.engine.classic.core.MasterReport; import org.pentaho.reporting.platform.plugin.SimpleReportingComponent; import org.saiku.adhoc.exceptions.SaikuAdhocException; import org.saiku.adhoc.messages.Messages; import org.saiku.adhoc.model.master.ReportTemplate; import org.saiku.adhoc.model.master.ReportTemplateStandalone; import org.saiku.adhoc.providers.IPrptProvider; import org.saiku.adhoc.service.report.SaikuReportingComponent; public class StandalonePrptProvider implements IPrptProvider { private URL repoURL; private Map<String, ReportTemplate> datasources = new HashMap<String, ReportTemplate>(); public StandalonePrptProvider() { } public StandalonePrptProvider(String path) { try { setPath(path); } catch (Exception e) { e.printStackTrace(); } } public void setPath(String path) { FileSystemManager fileSystemManager; try { fileSystemManager = VFS.getManager(); FileObject fileObject; fileObject = fileSystemManager.resolveFile(path); if (fileObject == null) { throw new IOException("File cannot be resolved: " + path); } if (!fileObject.exists()) { throw new IOException("File does not exist: " + path); } repoURL = fileObject.getURL(); if (repoURL == null) { throw new Exception("Cannot load connection repository from path: " + path); } else { load(); } } catch (Exception e) { e.printStackTrace(); } } public void load() { datasources.clear(); try { if (repoURL != null) { File[] files = new File(repoURL.getFile()).listFiles(); for (File file : files) { if (!file.isHidden()) { if (getFileExtension(file.getAbsolutePath()) != null && getFileExtension(file.getAbsolutePath()).equalsIgnoreCase("prpt")) { // Properties props = new Properties(); // props.load(new FileInputStream(file)); // String name = props.getProperty("name"); // String type = props.getProperty("type"); // if (name != null && type != null) { // Type t = ReportTemplate.Type.valueOf(type.toUpperCase()); ReportTemplate ds = new ReportTemplateStandalone(null, FilenameUtils.getFullPath(file.getPath()), FilenameUtils.removeExtension(file.getName())); datasources.put(FilenameUtils.removeExtension(file.getName()), ds); //} } } } } else { throw new Exception("repo URL is null"); } } catch (Exception e) { System.out.println(e.getMessage()); } } private static String getFileExtension(String filePath) { File f = new File(filePath); String name = f.getName(); int k = name.lastIndexOf("."); String ext = null; if (k != -1) ext = name.substring(k + 1, name.length()); return ext; } public ReportTemplate addDatasource(ReportTemplate datasource) { return null; // try { // String uri = repoURL.toURI().toString(); // if (uri != null && datasource != null) { // uri += datasource.getName().replace(" ", "_"); // File dsFile = new File(new URI(uri)); // if (dsFile.exists()) { // dsFile.delete(); // } else { // dsFile.createNewFile(); // } // FileWriter fw = new FileWriter(dsFile); // Properties props = datasource.getProperties(); // props.store(fw, null); // fw.close(); // datasources.put(datasource.getName(), datasource); // return datasource; // // } else { // throw new Exception("Cannot save datasource because uri or datasource is null uri(" // + (uri == null) + ")"); // } // } catch (Exception e) { // try { // throw new Exception(e); // } catch (Exception e1) { // // TODO Auto-generated catch block // e1.printStackTrace(); // } // } // return datasource; } public ReportTemplate setDatasource(ReportTemplate datasource) { return addDatasource(datasource); } public List<ReportTemplate> addDatasources(List<ReportTemplate> datasources) { for (ReportTemplate ds : datasources) { addDatasource(ds); } return datasources; } public boolean removeDatasource(String datasourceName) { try { String uri = repoURL.toURI().toString(); if (uri != null) { // seems like we don't have to do this anymore // uri.toString().endsWith(String.valueOf(File.separatorChar))) { uri += datasourceName; File dsFile = new File(new URI(uri)); if (dsFile.delete()) { datasources.remove(datasourceName); } } else { throw new Exception("Cannot delete datasource file uri:" + uri); } } catch (Exception e) { try { throw new Exception("Cannot delete datasource", e); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } return false; } public Map<String, ReportTemplate> getDatasources() { return datasources; } public ReportTemplate getDatasource(String datasourceName) { // if(!datasourceName.substring(datasourceName.length()-4).equals("PRPT")) // { // return datasources.get(datasourceName+".prpt"); // }else{ return datasources.get(datasourceName); //} } @Override public String getPath() { return repoURL.getPath(); } @Override public String getSolution() { return ""; } @Override public String getTemplatePath() { // TODO Auto-generated method stub return repoURL.getPath(); } @Override public ReportTemplate getTemplate(String path, String solution, String templateName) { return this.getDatasource(templateName); } @Override public SimpleReportingComponent getReportingComponent() { return new SaikuReportingComponent(); } @Override public MasterReport getMasterReport(String fullPath, SimpleReportingComponent reportComponent) throws SaikuAdhocException { FileInputStream in = null; try { //in = new FileInputStream(fullPath); reportComponent.setReportDefinitionPath(fullPath); //reportComponent.setReportDefinitionInputStream(in); return reportComponent.getReport(); } catch (Exception e) { throw new SaikuAdhocException(Messages.getErrorString("Repository.ERROR_0001_PRPT_TEMPLATE_NOT_FOUND")); } } @Override public MasterReport getPrptTemplate(ReportTemplate reportTemplate) { try { String path = reportTemplate.getPath() + reportTemplate.getName() + ".prpt"; return getMasterReport(path, new SaikuReportingComponent()); } catch (SaikuAdhocException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }