1 19 20 package za.org.coefficient.modules.configuration; 21 22 import net.sf.hibernate.Hibernate; 23 import net.sf.hibernate.util.HibernateUtil; 24 25 import za.org.coefficient.core.BaseConfigurationBackedConstants; 26 import za.org.coefficient.core.CoefficientConfiguration; 27 import za.org.coefficient.core.Constants; 28 import za.org.coefficient.authentication.Role; 29 import za.org.coefficient.interfaces.CoefficientContext; 30 import za.org.coefficient.modules.BaseModule; 31 import za.org.coefficient.util.ejb.SecurityUtil; 32 import za.org.coefficient.util.ejb.VelocityScreenUtil; 33 34 import java.util.HashMap ; 35 import java.util.List ; 36 37 51 public class Configuration extends BaseModule { 52 54 public String getMainMethod() { 55 return "view"; 56 } 57 58 public String getModuleDescription() { 59 return "Module that is used to setup the configuration of the site"; 60 } 61 62 public String getModuleDisplayName() { 63 return "Configuration Administration"; 64 } 65 66 public String canExecuteForRole(CoefficientContext ctx, String methodName, 67 Role usersHighestRole) { 68 69 if (usersHighestRole != null) { 71 if (usersHighestRole.getRoleValue() != SecurityUtil.SITE_ADMIN_ROLE_VAL 72 && Constants.hasRequiredFields()) { 73 return "Only a site administrator can change site settings"; 74 } else { 75 return null; 76 } 77 } else { 78 return null; 79 } 80 } 81 82 public CoefficientContext view(CoefficientContext ctx) throws Exception { 83 HashMap map = new HashMap (); 84 CoefficientConfiguration cc = 85 getConfiguration("za.org.coefficient.core.Constants"); 86 map.put("configuration", cc); 87 BaseConfigurationBackedConstants conf = null; 88 try { 89 conf = (BaseConfigurationBackedConstants)Class 90 .forName(cc.getForClass()).newInstance(); 91 } catch(Exception e) { 92 } 94 if(conf != null) { 95 map.put("properties", conf.getPropertyFields("za.org.coefficient.core.Constants")); 96 if (Constants.hasRequiredFields()) { 97 map.put("hasRequiredFields", new Boolean (true)); 98 } 99 } 100 StringBuffer sb = 101 VelocityScreenUtil.getProcessedScreen("view.vm", map); 102 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 103 return ctx; 104 } 105 106 public CoefficientContext save(CoefficientContext ctx) throws Exception { 107 Long id = ctx.getParameterAsLong("configId", -1); 108 if (id.longValue() > -1) { 109 CoefficientConfiguration cc = (CoefficientConfiguration) 110 HibernateUtil.load(CoefficientConfiguration.class, id); 111 int i = 0; 112 String propertyName = ctx.getParameter("propertyName"+i); 113 String propertyValue = ctx.getParameter("propertyValue"+i); 114 while (propertyName != null && propertyValue != null) { 115 cc.addProperty(propertyName, propertyValue); 116 i++; 117 propertyValue = ctx.getParameter("propertyValue"+i); 118 propertyName = ctx.getParameter("propertyName"+i); 119 } 120 HibernateUtil.update(cc); 121 try { 122 BaseConfigurationBackedConstants conf = 123 (BaseConfigurationBackedConstants)Class 124 .forName(cc.getForClass()).newInstance(); 125 conf.initFromConfig(cc, "za.org.coefficient.core.Constants"); 126 } catch(Exception e) { 127 } 129 ctx.setModuleContent("Configuration saved", getModuleDisplayName()); 131 } else { 132 ctx.setError("You must provide a configuration to save"); 133 } 134 return ctx; 135 } 136 137 141 public CoefficientConfiguration getConfiguration(String forClass) 142 throws Exception { 143 CoefficientConfiguration cc = null; 144 List config = HibernateUtil.find("from " 145 + CoefficientConfiguration.class.getName() 146 +" as conf where conf.forClass = ?", 147 forClass, Hibernate.STRING); 148 if (config.size() == 1) { 149 cc = (CoefficientConfiguration)config.get(0); 150 } else { 151 cc = new CoefficientConfiguration(); 152 cc.setForClass(forClass); 153 HibernateUtil.save(cc); 154 } 156 return cc; 157 } 158 159 public void saveConfiguration (CoefficientConfiguration cc) throws Exception { 160 HibernateUtil.update(cc); 161 } 163 } 164 | Popular Tags |