1 18 19 package cowsultants.itracker.web.actions; 20 21 import java.io.*; 22 import java.rmi.*; 23 import java.util.*; 24 import javax.ejb.*; 25 import javax.rmi.*; 26 import javax.naming.*; 27 import javax.servlet.*; 28 import javax.servlet.http.*; 29 30 import org.apache.commons.beanutils.*; 31 import org.apache.struts.action.*; 32 import org.apache.struts.upload.*; 33 import org.apache.struts.util.*; 34 35 import cowsultants.itracker.ejb.client.exceptions.*; 36 import cowsultants.itracker.ejb.client.interfaces.*; 37 import cowsultants.itracker.ejb.client.models.*; 38 import cowsultants.itracker.ejb.client.resources.*; 39 import cowsultants.itracker.ejb.client.util.*; 40 import cowsultants.itracker.web.util.*; 41 42 43 public class EditCustomFieldAction extends ITrackerAction { 44 45 public EditCustomFieldAction() { 46 } 47 48 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 49 ActionErrors errors = new ActionErrors(); 50 51 if(! isLoggedIn(request, response)) { 52 return mapping.findForward("login"); 53 } 54 if(! isTokenValid(request)) { 55 Logger.logDebug("Invalid request token while editing configuration."); 56 return mapping.findForward("listconfiguration"); 57 } 58 resetToken(request); 59 HttpSession session = request.getSession(true); 60 61 try { 62 InitialContext ic = new InitialContext(); 63 64 Object scRef = ic.lookup("java:comp/env/" + SystemConfiguration.JNDI_NAME); 65 SystemConfigurationHome scHome = (SystemConfigurationHome) PortableRemoteObject.narrow(scRef, SystemConfigurationHome.class); 66 SystemConfiguration sc = scHome.create(); 67 68 String action = (String ) PropertyUtils.getSimpleProperty(form, "action"); 69 70 if(action == null) { 71 return mapping.findForward("listconfiguration"); 72 } 73 74 CustomFieldModel customField = null; 75 if("create".equals(action)) { 76 customField = new CustomFieldModel(); 77 customField.setFieldType(((Integer ) PropertyUtils.getSimpleProperty(form, "fieldType")).intValue()); 78 customField.setRequired(("true".equals((String ) PropertyUtils.getSimpleProperty(form, "required")) ? true : false)); 79 customField.setSortOptionsByName(("true".equals((String ) PropertyUtils.getSimpleProperty(form, "sortOptionsByName")) ? true : false)); 80 customField.setDateFormat((String ) PropertyUtils.getSimpleProperty(form, "dateFormat")); 81 customField = sc.createCustomField(customField); 82 } else if("update".equals(action)) { 83 Integer id = (Integer ) PropertyUtils.getSimpleProperty(form, "id"); 84 customField = sc.getCustomField(id); 85 if(customField == null) { 86 throw new SystemConfigurationException("Invalid custom field id " + id); 87 } 88 customField.setFieldType(((Integer ) PropertyUtils.getSimpleProperty(form, "fieldType")).intValue()); 89 customField.setRequired(("true".equals((String ) PropertyUtils.getSimpleProperty(form, "required")) ? true : false)); 90 customField.setSortOptionsByName(("true".equals((String ) PropertyUtils.getSimpleProperty(form, "sortOptionsByName")) ? true : false)); 91 customField.setDateFormat((String ) PropertyUtils.getSimpleProperty(form, "dateFormat")); 92 customField.setOptions(null); 94 customField = sc.updateCustomField(customField); 95 } else { 96 throw new SystemConfigurationException("Invalid action " + action + " while editing custom field."); 97 } 98 99 if(customField == null) { 100 throw new SystemConfigurationException("Unable to create new custom field model."); 101 } 102 103 HashMap translations = (HashMap) PropertyUtils.getSimpleProperty(form, "translations"); 104 String key = CustomFieldUtilities.getCustomFieldLabelKey(customField.getId()); 105 Logger.logDebug("Processing label translations for custom field " + customField.getId() + " with key " + key); 106 if(translations != null && key != null && ! key.equals("")) { 107 for(Iterator iter = translations.keySet().iterator(); iter.hasNext(); ) { 108 String locale = (String ) iter.next(); 109 if(locale != null) { 110 String translation = (String ) translations.get(locale); 111 if(translation != null && ! translation.equals("")) { 112 Logger.logDebug("Adding new translation for locale " + locale + " for " + customField); 113 sc.updateLanguageItem(new LanguageModel(locale, key, translation)); 114 } 115 } 116 } 117 String baseValue = (String ) translations.get(ITrackerResources.BASE_LOCALE); 118 sc.updateLanguageItem(new LanguageModel(ITrackerResources.BASE_LOCALE, key, baseValue)); 119 ITrackerResources.clearKeyFromBundles(key, true); 120 } 121 122 sc.resetConfigurationCache(SystemConfigurationUtilities.TYPE_CUSTOMFIELD); 124 return mapping.findForward("listconfiguration"); 125 } catch(SystemConfigurationException sce) { 126 Logger.logError("Exception processing form data: " + sce.getMessage(), sce); 127 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(sce.getKey())); 128 } catch(Exception e) { 129 Logger.logError("Exception processing form data", e); 130 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.system")); 131 } 132 133 if(! errors.isEmpty()) { 134 saveErrors(request, errors); 135 saveToken(request); 136 return mapping.getInputForward(); 137 } 138 139 return mapping.findForward("error"); 140 } 141 } 142 | Popular Tags |