1 20 package org.openi.project; 21 22 import org.apache.log4j.LogManager; 23 import org.apache.log4j.Logger; 24 import org.openi.analysis.Datasource; 25 import org.openi.xml.BeanStorage; 26 import org.openi.xml.XMLTransformer; 27 import org.w3c.dom.*; 28 import java.io.*; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.LinkedList ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.TreeMap ; 35 import javax.xml.parsers.*; 36 import javax.xml.transform.*; 37 import javax.xml.transform.stream.*; 38 39 40 45 public class ProjectFactory { 46 private static Logger logger = LogManager.getLogger(ProjectFactory.class); 47 48 private ProjectFactory() { 49 } 50 51 58 59 75 public static Map getProjectMap(String baseDirName) { 76 Map projectMap = new TreeMap (); 77 File baseDir = new File(baseDirName); 78 79 File[] subDirs = baseDir.listFiles(new ProjectDirectoryFilter()); 81 82 for (int i = 0; i < subDirs.length; i++) { 83 try { 84 Project current = getProject(baseDirName, subDirs[i].getName()); 85 projectMap.put(current.getProjectId(), current); 86 } catch (IOException e) { 87 logger.warn("could not restore project, but continuing to build map. Is this supposed to be a project? " + 88 e.getMessage()); 89 } 90 } 91 92 return projectMap; 93 } 94 95 103 public static Map getProjectsByCategory(String baseDirName) { 104 return getProjectsByCategory(baseDirName, null); 105 } 106 107 122 public static Map getProjectsByCategory(String baseDirName, String username) { 123 long start = System.currentTimeMillis(); 124 Map categories = new TreeMap (); 125 Map rawProjects = getProjectMap(baseDirName); 126 Iterator projects = rawProjects.values().iterator(); 127 128 while (projects.hasNext()) { 129 Project current = (Project) projects.next(); 130 131 if (current != null) { 133 if ((username == null) || current.validateAdmin(username) 136 || current.validateUser(username)) { 137 String categoryName = current.getCategory(); 140 141 if (categoryName == null) { 142 categoryName = "General"; 143 } 144 145 Category category = (Category) categories.get(categoryName); 146 147 if (category == null) { 148 category = new Category(categoryName); 149 categories.put(categoryName, category); 150 } 151 152 category.addProject(current); 153 } 154 } 155 } 156 157 logger.info("completed creating project by category in: " 158 + (System.currentTimeMillis() - start) + " ms"); 159 160 return categories; 161 } 162 163 171 public static Project getProject(String baseDirName, String projectId) 172 throws IOException { 173 if (baseDirName == null) { 175 throw new IOException( 176 "Project directory name is null. This should be the location for all projects"); 177 } 178 179 BeanStorage storage = new BeanStorage(); 180 181 String projectFilePath = new File(baseDirName, 182 projectId + "/project.xml").getCanonicalPath(); 183 logger.debug("Trying to restore project: " + projectFilePath); 184 185 Project retProject = null; 186 try{ 187 retProject = (Project) storage.restoreBeanFromFile(projectFilePath); 188 }catch(Exception e){ 189 logger.debug("trouble restoring project from " + projectFilePath, e); 190 logger.debug("possibly old format, trying xslt"); 191 } 192 193 if(retProject == null){ 194 String xsltPath = new File(baseDirName).getParent() 195 + "/openi/WEB-INF/project/project.xsl"; 196 logger.debug( 197 "transforming project.xml into new format using template " 198 + xsltPath); 199 200 try { 201 String backup = backupOldFile(projectFilePath); 202 transformProjectToNewFormat(backup, projectFilePath, xsltPath); 203 retProject = (Project) storage.restoreBeanFromFile(projectFilePath); 205 } catch (Exception ex) { 206 throw new IOException( 208 "An error occured while transforming project.xml into new format\nroot cause:" 209 + ex.getMessage()); 210 } 211 } 212 213 return retProject; 214 } 215 216 217 218 private static String backupOldFile(String oldFileName) { 219 logger.debug("trying to backup filename: " + oldFileName); 220 String newfilename = null; 221 try{ 222 File oldFile = new File(oldFileName); 223 for(int i=0; i < 100; i++){ 225 newfilename = oldFile.getCanonicalPath() + "." + i; 226 File newFile = new File(newfilename); 227 if(!newFile.exists()){ 228 logger.debug("backing up project to " + newfilename); 229 oldFile.renameTo(newFile); 230 break; 231 } 232 } 233 }catch(Exception e){ 234 logger.error(e); 236 } 237 return newfilename; 238 } 239 240 private static void transformProjectToNewFormat(String backup, String projectXml, 241 String xslFile) throws Exception { 242 String result = XMLTransformer.transform(new FileInputStream(xslFile), 243 new FileInputStream(backup)); 244 245 FileOutputStream out = new FileOutputStream(projectXml); 246 out.write(result.getBytes()); 247 out.close(); 248 } 249 250 256 public static Project createProject(String baseDirName, String projectId) 257 throws IOException { 258 Project project = null; 259 260 File baseDirTest = new File(baseDirName); 262 263 if (baseDirTest == null) { 264 throw new FileNotFoundException("invalid base directory: " 265 + baseDirName); 266 } else { 267 if (!baseDirTest.exists()) { 268 throw new FileNotFoundException("invalid base directory: " 269 + baseDirName); 270 } 271 } 272 273 try { 275 project = getProject(baseDirName, projectId); 276 } catch (IOException e) { 277 } 279 280 if (project != null) { 281 throw new IllegalArgumentException ( 282 "Cannot create, project already exists: " + projectId); 283 } else { 284 project = new Project(); 286 project.setProjectId(projectId); 287 project.setProjectName(projectId); 288 project.setLogoUrl("images/logo.gif"); 289 project.setSplashImageUrl("images/splash.gif"); 290 291 Datasource ds = new Datasource(); 292 ds.setServer(""); 293 294 Map dsMap = new HashMap (); 295 dsMap.put("projectdefault", ds); 296 project.setDataSourceMap(dsMap); 297 298 List modules = new LinkedList (); 300 modules.add("public"); 301 project.setModules(modules); 302 303 BeanStorage storage = new BeanStorage(); 305 storage.saveBeanToFile(baseDirName + "/" + projectId 306 + "/project.xml", project); 307 } 308 309 return project; 310 } 311 } 312 | Popular Tags |