1 19 20 package za.org.coefficient.modules.project; 21 22 import org.apache.commons.lang.StringUtils; 23 24 import za.co.csir.icomtek.workflow.WorkflowData; 25 import za.co.csir.icomtek.workflow.WorkflowEvaluator; 26 27 import za.org.coefficient.authentication.Role; 28 import za.org.coefficient.core.Project; 29 import za.org.coefficient.html.ThemeSupport; 30 import za.org.coefficient.interfaces.CoefficientContext; 31 import za.org.coefficient.modules.BaseModule; 32 import za.org.coefficient.util.common.InvokerFactory; 33 import za.org.coefficient.util.ejb.VelocityScreenUtil; 34 35 import java.io.InputStream ; 36 import java.io.IOException ; 37 38 import java.util.HashMap ; 39 import java.util.Iterator ; 40 41 55 public class ProjectModuleLinks extends BaseModule { 56 57 public String getMainMethod() { 58 return "renderModuleLinks"; 59 } 60 61 public String getModuleDescription() { 62 return "This will show a link bar to " + ProjectConstants.PROJECT_NAME 63 + " modules that exist in the current " 64 + ProjectConstants.PROJECT_NAME; 65 } 66 67 public String getModuleDisplayName() { 68 return StringUtils.capitalise(ProjectConstants.PROJECT_NAME) 69 + " Links"; 70 } 71 72 public String canExecuteForRole(CoefficientContext ctx, String methodName, 73 Role usersHighestRole) { 74 return null; 76 } 77 78 88 public CoefficientContext renderModuleLinks(CoefficientContext ctx) { 89 Project prj = ctx.getProject(); 90 91 if(prj != null && prj.getActive()) { 94 HashMap modules = new HashMap (); 95 HashMap summaries = new HashMap (); 96 String modName = null; 97 try { 98 WorkflowData wd = (WorkflowData)ctx 99 .getSessionAttribute("__current_workflow_data__"); 100 101 for (Iterator it = prj.getModules().iterator(); it.hasNext();) { 103 modName = (String ) it.next(); 104 if (!prj.getIsWorkflow() 105 || WorkflowEvaluator.isModuleViewableForState(ctx, wd, modName)) { 106 String displayName = (String )InvokerFactory.getInvoker() 107 .invokeGetterOnModule(modName, "moduleDisplayName"); 108 String summary = (String )InvokerFactory.getInvoker() 109 .invokeOpOnModule(modName, "getSummaryForProject", 110 ctx); 111 modules.put(modName, displayName); 112 summaries.put(modName, summary); 113 } 114 } 115 116 } catch (Exception e) { 117 System.err.println("There was an error getting the display name of module: " + modName); 118 } 119 120 HashMap map = new HashMap (); 121 HashMap extensions = new HashMap (); 122 123 if(ProjectConstants.RENDER_MODULE_LINKS_HORIZONTAL) { 125 int width = 100 / (modules.size() + 1); 126 map.put("td_width", new Integer (width)); 127 } 128 129 String resourcePath = ThemeSupport.getCurrentPathToThemeResource(); 130 map.put("resourcePath", resourcePath); 131 map.put("modules", modules); 132 map.put("project", prj); 133 map.put("summaries", summaries); 134 map.put("hasImages", hasAtLeastOneImage(modules, extensions, resourcePath). 135 toString()); 136 map.put("extensions", extensions); 137 if(ProjectConstants.RENDER_MODULE_LINKS_HORIZONTAL) { 138 map.put("horizontal", "true"); 139 } 140 141 StringBuffer sb = 142 VelocityScreenUtil.getProcessedScreen("renderModuleLinks.vm", 143 map); 144 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 145 } 146 return ctx; 147 } 148 149 private Boolean hasAtLeastOneImage(HashMap modules, HashMap extensions, 150 String pathToResource) { 151 boolean hasImage = false; 152 HashMap modulesPlus = new HashMap (modules); 153 String [] imageExtensions = new String [] {".gif", ".jpg", ".png"}; 154 modulesPlus.put("Project", "Home"); 155 for(Iterator it = modulesPlus.keySet().iterator(); it.hasNext(); ) { 156 String moduleName = (String )it.next(); 157 for(int i = 0; i < imageExtensions.length; i++) { 158 String imageURL = "/" + pathToResource + "/" + moduleName 159 + imageExtensions[i]; 160 try { 161 InputStream is = ProjectModuleLinks.class 162 .getResourceAsStream(imageURL); 163 if(is != null) { 164 is.close(); 165 hasImage = true; 166 extensions.put(moduleName, imageExtensions[i]); 167 break; 168 } 169 } catch(IOException ioe) { 170 ioe.printStackTrace(); 171 } 173 } 174 } 175 return new Boolean (hasImage); 176 } 177 178 } | Popular Tags |