Example usage for org.apache.commons.beanutils PropertyUtils getSimpleProperty

List of usage examples for org.apache.commons.beanutils PropertyUtils getSimpleProperty

Introduction

In this page you can find the example usage for org.apache.commons.beanutils PropertyUtils getSimpleProperty.

Prototype

public static Object getSimpleProperty(Object bean, String name)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Return the value of the specified simple property of the specified bean, with no type conversions.

For more details see PropertyUtilsBean.

Usage

From source file:org.itracker.web.actions.admin.configuration.EditCustomFieldValueAction.java

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    ActionMessages errors = new ActionMessages();

    if (!isTokenValid(request)) {
        log.debug("Invalid request token while editing configuration.");
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.transaction"));
        saveErrors(request, errors);//  w  ww  . j  a v a2 s. c  om
        return mapping.findForward("listconfiguration");
    }
    resetToken(request);
    HttpSession session = request.getSession(true);
    CustomField customField = (CustomField) session.getAttribute(Constants.CUSTOMFIELD_KEY);
    if (customField == null) {
        return mapping.findForward("listconfiguration");
    }

    CustomFieldValueForm customFieldValueForm = (CustomFieldValueForm) form;

    try {

        String action = customFieldValueForm.getAction();

        if (action == null) {
            return mapping.findForward("listconfiguration");
        }

        ConfigurationService configurationService = getITrackerServices().getConfigurationService();
        CustomFieldValue customFieldValue;

        if ("create".equals(action)) {
            customFieldValue = new CustomFieldValue();
            customFieldValue.setCustomField(customField);
            customFieldValue.setValue(customFieldValueForm.getValue());
            customFieldValue.setSortOrder(customFieldValueForm.getSortOrder());
            customFieldValue = configurationService.createCustomFieldValue(customFieldValue);
        } else if ("update".equals(action)) {
            Integer id = (Integer) PropertyUtils.getSimpleProperty(form, "id");
            customFieldValue = configurationService.getCustomFieldValue(id);
            customFieldValue.setValue(customFieldValueForm.getValue());
            customFieldValue.setSortOrder(customFieldValueForm.getSortOrder());
            customFieldValue = configurationService.updateCustomFieldValue(customFieldValue);
        } else {
            throw new SystemConfigurationException(
                    "Invalid action " + action + " while editing custom field value.");
        }

        if (customFieldValue == null) {
            throw new SystemConfigurationException("Unable to create new custom field value model.");
        }

        Map<String, String> translations = customFieldValueForm.getTranslations();
        String key = CustomFieldUtilities.getCustomFieldOptionLabelKey(customField.getId(),
                customFieldValue.getId());
        log.debug("Processing label translations for custom field value " + customFieldValue.getId()
                + " with key " + key);
        if (translations != null && key != null && !key.equals("")) {
            for (String locale : translations.keySet()) {
                if (locale != null) {
                    String translation = translations.get(locale);
                    if (translation != null && !translation.equals("")) {
                        log.debug("Adding new translation for locale " + locale + " for "
                                + String.valueOf(customFieldValue.getId()));
                        configurationService.updateLanguageItem(new Language(locale, key, translation));
                    }
                }
            }
            String baseValue = translations.get(ITrackerResources.BASE_LOCALE);
            configurationService
                    .updateLanguageItem(new Language(ITrackerResources.BASE_LOCALE, key, baseValue));
        }
        if (key != null)
            ITrackerResources.clearKeyFromBundles(key, true);
        // Now reset the cached versions in IssueUtilities
        configurationService.resetConfigurationCache(Configuration.Type.customfield);
        request.setAttribute("action", action);
        String pageTitleKey = "";
        String pageTitleArg = "";
        pageTitleKey = "itracker.web.admin.editcustomfieldvalue.title.create";
        if ("update".equals(action)) {
            pageTitleKey = "itracker.web.admin.editcustomfieldvalue.title.update";
        }

        request.setAttribute("languages", configurationService.getAvailableLanguages());
        request.setAttribute("pageTitleKey", pageTitleKey);
        request.setAttribute("pageTitleArg", pageTitleArg);

        saveToken(request);
        return new ActionForward(mapping.findForward("editcustomfield").getPath() + "?id=" + customField.getId()
                + "&action=update");
    } catch (SystemConfigurationException sce) {
        log.error("Exception processing form data: " + sce.getMessage(), sce);
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(sce.getKey()));
    } catch (Exception e) {
        log.error("Exception processing form data", e);
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
    }

    if (!errors.isEmpty()) {
        saveErrors(request, errors);
        saveToken(request);
        request.setAttribute("customFieldValueForm", form);
        return mapping.getInputForward();
    }

    return mapping.findForward("error");
}

From source file:org.itracker.web.actions.admin.configuration.ImportDataVerifyAction.java

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    ActionMessages errors = new ActionMessages();

    if (!LoginUtilities.hasPermission(PermissionType.USER_ADMIN, request, response)) {
        return mapping.findForward("unauthorized");
    }//w w w .  j  av a  2  s.com

    try {
        FormFile file = (FormFile) PropertyUtils.getSimpleProperty(form, "importFile");
        ImportDataModel model = new ImportDataModel();
        AbstractEntity[] importData = ImportExportUtilities
                .importIssues(new InputStreamReader(file.getInputStream()));
        AbstractEntity[] existingModel = new AbstractEntity[importData.length];

        model.setReuseUsers((Boolean) PropertyUtils.getSimpleProperty(form, "optionreuseusers"));
        model.setReuseProjects((Boolean) PropertyUtils.getSimpleProperty(form, "optionreuseprojects"));
        model.setReuseConfig((Boolean) PropertyUtils.getSimpleProperty(form, "optionreuseconfig"));
        model.setCreatePasswords((Boolean) PropertyUtils.getSimpleProperty(form, "optioncreatepasswords"));
        model.setData(importData, existingModel);

        InitialContext ic = new InitialContext();
        checkConfig(model, ic);
        log.debug(model.toString());
        checkUsers(model, ic);
        log.debug(model.toString());
        checkProjects(model, ic);
        log.debug(model.toString());
        checkIssues(model, ic);
        log.debug(model.toString());

        HttpSession session = request.getSession(true);
        session.setAttribute(Constants.IMPORT_DATA_KEY, model);
    } catch (ImportExportException iee) {
        if (iee.getType() == ImportExportException.TYPE_INVALID_LOGINS) {
            log.error("Invalid logins found while verifying import data.");
            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("itracker.web.error.importexport.invalidlogins", iee.getMessage()));
        } else if (iee.getType() == ImportExportException.TYPE_INVALID_STATUS) {
            log.error("Invalid status found while verifying import data.");
            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("itracker.web.error.importexport.invalidstatus", iee.getMessage()));
        } else {
            log.error("Exception while verifying import data.", iee);
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
        }
    } catch (Exception e) {
        log.error("Exception while verifying import data.", e);
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
    }

    if (!errors.isEmpty()) {
        saveErrors(request, errors);
        return mapping.getInputForward();
    }

    return mapping.findForward("importdataverify");
}

From source file:org.itracker.web.actions.admin.configuration.OrderConfigurationItemAction.java

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    ActionMessages errors = new ActionMessages();

    if (!LoginUtilities.hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
        return mapping.findForward("unauthorized");
    }/*w  w  w .j  a va2s  .  c  om*/

    try {
        ConfigurationService configurationService = ServletContextUtils.getItrackerServices()
                .getConfigurationService();

        Integer configId = (Integer) PropertyUtils.getSimpleProperty(form, "id");
        String action = (String) PropertyUtils.getSimpleProperty(form, "action");
        if (configId == null || configId.intValue() <= 0) {
            throw new SystemConfigurationException("Invalid configuration id.");
        }

        Configuration configItem = configurationService.getConfigurationItem(configId);
        if (configItem == null) {
            throw new SystemConfigurationException("Invalid configuration id.");
        }

        Type configType = configItem.getType();
        List<Configuration> configItems = configurationService.getConfigurationItemsByType(configType);
        List<Configuration> newConfigItems = new ArrayList<Configuration>();

        for (int i = 0; i < configItems.size(); i++) {
            newConfigItems.add(configItems.get(i));
        }
        for (int i = 0; i < configItems.size(); i++) {
            if (configItems.get(i) != null) {
                Configuration firstConfiguration = new Configuration();
                Configuration secondConfiguration = new Configuration();
                Configuration curConfiguration = configItems.get(i);
                int todo_i = -1;
                if (curConfiguration.getId().equals(configId)) {
                    if ("up".equals(action)) {
                        todo_i = i - 1;
                    } else {
                        todo_i = i + 1;
                    }
                    Configuration todoConfiguration = configItems.get(todo_i);

                    firstConfiguration.setId(todoConfiguration.getId());
                    firstConfiguration.setCreateDate(todoConfiguration.getCreateDate());
                    firstConfiguration.setLastModifiedDate(todoConfiguration.getLastModifiedDate());
                    firstConfiguration.setName(todoConfiguration.getName());
                    firstConfiguration.setOrder(curConfiguration.getOrder());
                    firstConfiguration.setType(todoConfiguration.getType());
                    firstConfiguration.setValue(todoConfiguration.getValue());
                    firstConfiguration.setVersion(todoConfiguration.getVersion());

                    secondConfiguration.setId(curConfiguration.getId());
                    secondConfiguration.setCreateDate(curConfiguration.getCreateDate());
                    secondConfiguration.setLastModifiedDate(curConfiguration.getLastModifiedDate());
                    secondConfiguration.setName(curConfiguration.getName());
                    secondConfiguration.setOrder(todoConfiguration.getOrder());
                    secondConfiguration.setType(curConfiguration.getType());
                    secondConfiguration.setValue(curConfiguration.getValue());
                    secondConfiguration.setVersion(curConfiguration.getVersion());

                    newConfigItems.set(todo_i, firstConfiguration);
                    newConfigItems.set(i, secondConfiguration);
                }
            }
        }

        newConfigItems = configurationService.updateConfigurationItems(newConfigItems, configType);

        // Only resolutions and severities can be reordered at this point.  Statuses
        // and some basic workflow depend on the actual value of the status, so
        // the order must equal the value of the status for it to work correctly.
        switch (configType) {
        case resolution:
        case severity:
            configurationService.resetConfigurationCache(configType);
        }

        return mapping.findForward("listconfiguration");
    } catch (SystemConfigurationException nfe) {
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidconfiguration"));
        log.debug("Invalid configuration item id " + request.getParameter("id") + " specified.");
    } catch (NumberFormatException nfe) {
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidconfiguration"));
        log.debug("Invalid configuration item id " + request.getParameter("id") + " specified.");
    } catch (Exception e) {
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
        log.error("System Error.", e);
    }
    if (!errors.isEmpty()) {
        saveErrors(request, errors);
    }
    return mapping.findForward("error");
}

From source file:org.itracker.web.actions.admin.configuration.OrderCustomFieldValueAction.java

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    ActionMessages errors = new ActionMessages();

    if (!LoginUtilities.hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
        return mapping.findForward("unauthorized");
    }//from w w  w. j  a va 2 s. com

    try {
        ConfigurationService configurationService = ServletContextUtils.getItrackerServices()
                .getConfigurationService();

        Integer customFieldValueId = (Integer) PropertyUtils.getSimpleProperty(form, "id");
        String action = (String) PropertyUtils.getSimpleProperty(form, "action");
        if (customFieldValueId == null || customFieldValueId.intValue() <= 0) {
            throw new SystemConfigurationException("Invalid custom field value id.");
        }

        CustomFieldValue customFieldValue = configurationService.getCustomFieldValue(customFieldValueId);
        if (customFieldValue == null) {
            throw new SystemConfigurationException("Invalid custom field value id.");
        }

        CustomField customField = configurationService
                .getCustomField(customFieldValue.getCustomField().getId());
        if (customField == null) {
            throw new SystemConfigurationException("Invalid custom field id.");
        }
        List<CustomFieldValue> customFieldvalues = customField.getOptions();

        Collections.sort(customFieldvalues, CustomFieldValue.SORT_ORDER_COMPARATOR);
        Iterator<CustomFieldValue> valuesIt = customFieldvalues.iterator();
        CustomFieldValue curCustomFieldValue, prevCustomFieldValue = null;
        int i = 0;
        while (valuesIt.hasNext()) {
            curCustomFieldValue = valuesIt.next();
            curCustomFieldValue.setSortOrder(i);

            if (curCustomFieldValue.getId() == customFieldValueId) {

                if ("up".equals(action)) {
                    if (prevCustomFieldValue != null) {
                        curCustomFieldValue.setSortOrder(i - 1);
                        prevCustomFieldValue.setSortOrder(i);
                    }
                } else {
                    CustomFieldValue value = valuesIt.next();
                    value.setSortOrder(i++);
                    curCustomFieldValue.setSortOrder(i);
                    curCustomFieldValue = value;
                }

            }
            prevCustomFieldValue = curCustomFieldValue;
            i++;
        }

        configurationService.updateCustomField(customField);

        configurationService.resetConfigurationCache(Configuration.Type.customfield);
        request.setAttribute("action", action);
        return new ActionForward(mapping.findForward("editcustomfield").getPath() + "?id=" + customField.getId()
                + "&action=update");
    } catch (SystemConfigurationException nfe) {
        errors.add(ActionMessages.GLOBAL_MESSAGE,
                new ActionMessage("itracker.web.error.invalidcustomfieldvalue"));
        log.debug("Invalid custom field value id " + request.getParameter("id") + " specified.");
    } catch (NumberFormatException nfe) {
        errors.add(ActionMessages.GLOBAL_MESSAGE,
                new ActionMessage("itracker.web.error.invalidcustomfieldvalue"));
        log.debug("Invalid custom field value id " + request.getParameter("id") + " specified.");
    } catch (Exception e) {
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
        log.error("System Error.", e);
    }
    if (!errors.isEmpty()) {
        saveErrors(request, errors);
    }
    return mapping.findForward("error");
}

From source file:org.itracker.web.actions.admin.configuration.RemoveConfigurationItemAction.java

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    ActionMessages errors = new ActionMessages();

    if (!hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
        return mapping.findForward("unauthorized");
    }/*from  w w  w.ja v  a  2  s.  c  o  m*/

    try {
        ConfigurationService configurationService = getITrackerServices().getConfigurationService();

        Integer configId = (Integer) PropertyUtils.getSimpleProperty(form, "id");
        if (configId == null || configId <= 0) {
            throw new SystemConfigurationException("Invalid configuration id.");
        }

        Configuration configItem = configurationService.getConfigurationItem(configId);
        if (configItem == null) {
            throw new SystemConfigurationException("Invalid configuration id.");
        }

        String key;
        if (configItem.getType() == Configuration.Type.severity) {
            key = ITrackerResources.KEY_BASE_SEVERITY + configItem.getValue();

            // Need to promote all issues with the deleted severity.  The safest thing to do is
            // promote them to the next higher severity.
            try {
                String currConfigValue = configItem.getValue();
                String newConfigValue = null;

                List<Configuration> configItems = configurationService
                        .getConfigurationItemsByType(Configuration.Type.severity);
                for (int i = 0; i < configItems.size(); i++) {
                    if (configItems.get(i) != null && configId.equals(configItems.get(i).getId())) {
                        if (i == 0 && (i + 1) < configItems.size()) {
                            newConfigValue = configItems.get(i + 1).getValue();
                            break;
                        } else if (i > 0) {
                            newConfigValue = configItems.get(i - 1).getValue();
                            break;
                        }
                    }
                }

                int currSeverity = Integer.parseInt(currConfigValue);
                int newSeverity = Integer.parseInt(newConfigValue);
                log.debug("Promoting issues in severity " + IssueUtilities.getSeverityName(currSeverity)
                        + " to " + IssueUtilities.getSeverityName(newSeverity));

                HttpSession session = request.getSession(true);
                User currUser = (User) session.getAttribute(Constants.USER_KEY);
                Integer currUserId = (currUser == null ? -1 : currUser.getId());

                IssueService issueService = getITrackerServices().getIssueService();
                List<Issue> issues = issueService.getIssuesWithSeverity(currSeverity);
                for (int i = 0; i < issues.size(); i++) {
                    if (issues.get(i) != null) {
                        issues.get(i).setSeverity(newSeverity);

                        issues.set(i, issueService.systemUpdateIssue(issues.get(i), currUserId));
                    }
                }
            } catch (Exception e) {
                log.debug("Exception while promoting issues with severity " + configItem.getValue(), e);
            }
        } else if (configItem.getType() == Configuration.Type.status) {
            key = ITrackerResources.KEY_BASE_STATUS + configItem.getValue();

            // Need to demote all issues with the deleted severity.  The safest thing to do is
            // move them down one status to make sure they don't skip something important in any
            // workflow.

            try {
                String currConfigValue = configItem.getValue();
                String newConfigValue = null;

                List<Configuration> configItems = configurationService
                        .getConfigurationItemsByType(Configuration.Type.status);
                for (int i = 0; i < configItems.size(); i++) {
                    if (configItems.get(i) != null && configId.equals(configItems.get(i).getId())) {
                        if (i == 0 && (i + 1) < configItems.size()) {
                            newConfigValue = configItems.get(i + 1).getValue();
                            break;
                        } else if (i > 0) {
                            newConfigValue = configItems.get(i - 1).getValue();
                            break;
                        }
                    }
                }

                int currStatus = Integer.parseInt(currConfigValue);
                int newStatus = Integer.parseInt(newConfigValue);
                log.debug("Promoting issues in status " + IssueUtilities.getStatusName(currStatus) + " to "
                        + IssueUtilities.getStatusName(newStatus));

                HttpSession session = request.getSession(true);
                User currUser = (User) session.getAttribute(Constants.USER_KEY);
                Integer currUserId = (currUser == null ? -1 : currUser.getId());

                IssueService issueService = getITrackerServices().getIssueService();
                List<Issue> issues = issueService.getIssuesWithStatus(currStatus);
                for (int i = 0; i < issues.size(); i++) {
                    if (issues.get(i) != null) {
                        issues.get(i).setStatus(newStatus);

                        issues.set(i, issueService.systemUpdateIssue(issues.get(i), currUserId));
                    }
                }
            } catch (Exception e) {
                log.debug("Exception while promoting issues with status " + configItem.getValue(), e);
            }
        } else if (configItem.getType() == Configuration.Type.resolution) {
            key = ITrackerResources.KEY_BASE_RESOLUTION + configItem.getValue();

            // No need to edit any issues since the resolutions are stored as text in the issue
        } else {
            throw new SystemConfigurationException(
                    "Unsupported configuration item type " + configItem.getType().name() + " found.");
        }

        configurationService.removeConfigurationItem(configItem.getId());
        // Now reset the cached items of removed item's type
        configurationService.resetConfigurationCache(configItem.getType());
        configurationService.removeLanguageKey(key);
        ITrackerResources.clearKeyFromBundles(key, false);

        return mapping.findForward("listconfiguration");
    } catch (SystemConfigurationException sce) {
        log.debug(sce.getMessage(), sce);
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidconfiguration"));
    } catch (NumberFormatException nfe) {
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidconfiguration"));
        log.debug("Invalid configuration item id " + request.getParameter("id") + " specified.");
    } catch (Exception e) {
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
        log.error("System Error.", e);
    }
    if (!errors.isEmpty()) {
        saveErrors(request, errors);
    }
    return mapping.findForward("error");
}

From source file:org.itracker.web.actions.admin.configuration.RemoveCustomFieldAction.java

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    ActionMessages errors = new ActionMessages();

    if (!LoginUtilities.hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
        return mapping.findForward("unauthorized");
    }/*from   w w  w .j a v a  2 s.  c om*/

    try {
        ConfigurationService configurationService = ServletContextUtils.getItrackerServices()
                .getConfigurationService();

        Integer valueId = (Integer) PropertyUtils.getSimpleProperty(form, "id");
        if (valueId == null || valueId.intValue() <= 0) {
            throw new SystemConfigurationException("Invalid custom field id.");
        }

        CustomField customField = configurationService.getCustomField(valueId);
        if (customField == null) {
            throw new SystemConfigurationException("Invalid custom field id.");
        }
        String key = CustomFieldUtilities.getCustomFieldLabelKey(customField.getId());
        boolean status = configurationService.removeCustomField(customField.getId());
        if (status) {
            if (key != null)
                ITrackerResources.clearKeyFromBundles(key, false);
        } else {
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
        }

        configurationService.resetConfigurationCache(Configuration.Type.customfield);
        if (!errors.isEmpty()) {
            saveErrors(request, errors);
            return mapping.getInputForward();
        }
        return mapping.findForward("listconfiguration");
    } catch (SystemConfigurationException sce) {
        log.debug(sce.getMessage(), sce);
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidcustomfield"));
    } catch (NumberFormatException nfe) {
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidcustomfield"));
        log.debug("Invalid custom field  id " + request.getParameter("id") + " specified.");
    } catch (Exception e) {
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
        log.error("System Error.", e);
    }
    if (!errors.isEmpty()) {
        saveMessages(request, errors);
    }
    return mapping.findForward("error");
}

From source file:org.itracker.web.actions.admin.configuration.RemoveCustomFieldValueAction.java

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    ActionMessages errors = new ActionMessages();

    if (!LoginUtilities.hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
        return mapping.findForward("unauthorized");
    }//from  ww  w. j ava 2s . c  o  m

    try {
        ConfigurationService configurationService = ServletContextUtils.getItrackerServices()
                .getConfigurationService();

        Integer valueId = (Integer) PropertyUtils.getSimpleProperty(form, "id");
        if (valueId == null || valueId.intValue() <= 0) {
            throw new SystemConfigurationException("Invalid custom field value id.");
        }

        CustomFieldValue customFieldValue = configurationService.getCustomFieldValue(valueId);
        if (customFieldValue == null) {
            throw new SystemConfigurationException("Invalid custom field value id.");
        }

        String key = CustomFieldUtilities.getCustomFieldOptionLabelKey(
                customFieldValue.getCustomField().getId(), customFieldValue.getId());
        boolean status = configurationService.removeCustomFieldValue(customFieldValue.getId());

        if (status) {
            if (key != null) {
                ITrackerResources.clearKeyFromBundles(key, false);
            }
            configurationService.resetConfigurationCache(Configuration.Type.customfield);

            HttpSession session = request.getSession(true);
            CustomField customField = (CustomField) session.getAttribute(Constants.CUSTOMFIELD_KEY);
            if (customField == null) {
                return mapping.findForward("listconfiguration");
            }
            return new ActionForward(mapping.findForward("editcustomfield").getPath() + "?id="
                    + customField.getId() + "&action=update");
        } else {
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
        }

    } catch (SystemConfigurationException sce) {
        log.debug(sce.getMessage(), sce);
        errors.add(ActionMessages.GLOBAL_MESSAGE,
                new ActionMessage("itracker.web.error.invalidcustomfieldvalue"));
    } catch (NumberFormatException nfe) {
        errors.add(ActionMessages.GLOBAL_MESSAGE,
                new ActionMessage("itracker.web.error.invalidcustomfieldvalue"));
        log.debug("Invalid custom field value id " + request.getParameter("id") + " specified.");
    } catch (Exception e) {
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
        log.error("System Error.", e);
    }
    if (!errors.isEmpty()) {
        saveErrors(request, errors);
    }
    return mapping.findForward("error");
}

From source file:org.itracker.web.actions.admin.language.CreateLanguageKeyAction.java

@SuppressWarnings("unchecked")
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    ActionMessages errors = new ActionMessages();

    if (!isTokenValid(request)) {
        log.debug("Invalid request token while creating language key.");
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.transaction"));
        saveErrors(request, errors);//w  w  w .j  ava  2 s  .  c om
        return mapping.findForward("listlanguages");
    }
    resetToken(request);

    try {
        ConfigurationService configurationService = ServletContextUtils.getItrackerServices()
                .getConfigurationService();

        String key = (String) PropertyUtils.getSimpleProperty(form, "key");
        HashMap<String, String> items = (HashMap<String, String>) PropertyUtils.getSimpleProperty(form,
                "items");

        // Move to validation code
        if (items != null) {
            log.debug("Adding new language key: " + key);
            for (Iterator<String> iter = items.keySet().iterator(); iter.hasNext();) {
                String locale = iter.next();
                log.debug("Checking translation for locale " + locale);
                if (locale != null) {
                    String value = items.get(locale);
                    log.debug("Locale value: " + value);
                    if (value != null && !value.equals("")) {
                        log.debug("Adding new translation for locale " + locale + " for key " + key);
                        configurationService.updateLanguageItem(new Language(locale, key, value));
                    }
                }
            }
            String baseValue = (String) items.get(ITrackerResources.BASE_LOCALE);

            configurationService
                    .updateLanguageItem(new Language(ITrackerResources.BASE_LOCALE, key, baseValue));
            ITrackerResources.clearKeyFromBundles(key, true);
        }

        return mapping.findForward("listlanguages");
    } catch (Exception e) {
        log.error("Exception processing form data", e);
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
    }

    if (!errors.isEmpty()) {
        saveErrors(request, errors);
        saveToken(request);
        return mapping.getInputForward();
    }
    return mapping.findForward("error");
}

From source file:org.itracker.web.actions.admin.language.EditLanguageAction.java

@SuppressWarnings("unchecked")
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    ActionMessages errors = new ActionMessages();
    String action;/*www . j a v a  2 s  .  c o  m*/
    try {
        action = (String) PropertyUtils.getSimpleProperty(form, "action");
    } catch (Exception e) {
        log.error("Exception processing form data", e);
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
        return mapping.findForward("error");
    }
    if (!isTokenValid(request) && !"disable".equals(action)) {
        log.info("Invalid request token while editing language.");
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.transaction"));
        saveErrors(request, errors);
        return mapping.findForward("listlanguages");
    }
    resetToken(request);
    HttpSession session = request.getSession(true);

    try {
        ConfigurationService configurationService = ServletContextUtils.getItrackerServices()
                .getConfigurationService();

        String locale = (String) PropertyUtils.getSimpleProperty(form, "locale");
        String localeTitle = (String) PropertyUtils.getSimpleProperty(form, "localeTitle");
        String localeBaseTitle = (String) PropertyUtils.getSimpleProperty(form, "localeBaseTitle");
        HashMap<String, String> items = (HashMap<String, String>) PropertyUtils.getSimpleProperty(form,
                "items");

        if (items == null) {
            return mapping.findForward("listlanguages");
        }

        if (locale == null || "".equals(locale.trim())) {
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidlocale"));
        } else if ("disable".equals(action)) {
            // This will update the Base Locale to remove the new language.
            configurationService.getAvailableLanguages();
            configurationService.getAvailableLanguages();

            List<Configuration> localeConfigs = configurationService
                    .getConfigurationItemsByType(Configuration.Type.locale);

            for (Configuration configuration : localeConfigs) {
                if (configuration.getValue().startsWith(locale)) {
                    configurationService.removeConfigurationItem(configuration.getId());
                    ITrackerResources.clearBundles();
                    return mapping.findForward("listlanguages");
                }
            }

        } else if ("create".equals(action)) {

            if (locale.length() != 2 && (locale.length() != 5 || locale.indexOf('_') != 2)) {
                errors.add(ActionMessages.GLOBAL_MESSAGE,
                        new ActionMessage("itracker.web.error.invalidlocale"));
            } else {
                Language languageItem = configurationService.getLanguageItemByKey("itracker.locales", null);
                String localeString = languageItem.getResourceValue();
                languageItem.setResourceValue(localeString + "," + locale);
                configurationService.updateLanguageItem(languageItem);

                Configuration localeConfig = new Configuration(Configuration.Type.locale, locale);
                if (configurationService.configurationItemExists(localeConfig)) {
                    errors.add(ActionMessages.GLOBAL_MESSAGE,
                            new ActionMessage("itracker.web.error.invalidlocale"));
                } else {

                    configurationService
                            .updateLanguageItem(new Language(locale, "itracker.locale.name", localeTitle));
                    configurationService.updateLanguageItem(
                            new Language(locale, "itracker.locale.name." + locale, localeTitle));
                    configurationService.updateLanguageItem(new Language(ITrackerResources.BASE_LOCALE,
                            "itracker.locale.name." + locale, localeBaseTitle));
                    for (String key : items.keySet()) {
                        if (key != null) {
                            String value = items.get(key);
                            if (value != null && value.length() != 0) {
                                configurationService
                                        .updateLanguageItem(new Language(locale, key.replace('/', '.'), value));
                            }
                        }
                    }
                    configurationService.createConfigurationItem(localeConfig);
                    ITrackerResources.clearBundles();
                    clearSessionObjects(session);
                    return mapping.findForward("listlanguages");
                }
            }
        } else if ("update".equals(action)) {

            Locale updateLocale = ITrackerResources.getLocale(locale);
            for (String key : items.keySet()) {
                if (key != null) {
                    String value = items.get(key);
                    try {
                        String currValue = ITrackerResources.getCheckForKey(key.replace('/', '.'),
                                updateLocale);
                        if (value == null || value.length() == 0) {
                            try {
                                configurationService
                                        .removeLanguageItem(new Language(locale, key.replace('/', '.')));
                            } catch (NoSuchEntityException e) {
                                // do nothing; we want to delete it, so...
                            }

                        } else if (!value.equals(currValue)) {
                            configurationService
                                    .updateLanguageItem(new Language(locale, key.replace('/', '.'), value));
                        }
                    } catch (MissingResourceException mre) {
                        if (value != null && !value.trim().equals("")) {
                            configurationService
                                    .updateLanguageItem(new Language(locale, key.replace('/', '.'), value));
                        }
                    }
                }
            }

            configurationService.updateLanguageItem(new Language(locale, "itracker.locale.name", localeTitle));
            configurationService
                    .updateLanguageItem(new Language(locale, "itracker.locale.name." + locale, localeTitle));
            configurationService.updateLanguageItem(new Language(ITrackerResources.BASE_LOCALE,
                    "itracker.locale.name." + locale, localeBaseTitle));

            ITrackerResources.clearBundles();
            clearSessionObjects(session);
            return mapping.findForward("listlanguages");
        } else {
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidaction"));
        }
    } catch (Exception e) {
        log.error("Exception processing form data", e);
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
    }

    if (!errors.isEmpty()) {
        saveErrors(request, errors);
        saveToken(request);
        return mapping.getInputForward();
    }

    clearSessionObjects(session);
    return mapping.findForward("error");
}

From source file:org.itracker.web.actions.admin.language.EditLanguageFormAction.java

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    ActionMessages errors = new ActionMessages();

    try {/*w  w w  .  j a v  a 2 s . co  m*/
        ConfigurationService configurationService = ServletContextUtils.getItrackerServices()
                .getConfigurationService();

        HttpSession session = request.getSession(true);

        LanguageForm languageForm = (LanguageForm) form;
        if (languageForm == null) {
            languageForm = new LanguageForm();
        }

        String locale = (String) PropertyUtils.getSimpleProperty(form, "locale");
        int localeType = SystemConfigurationUtilities.getLocaleType(locale);
        if (localeType == SystemConfigurationUtilities.LOCALE_TYPE_INVALID) {
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidlocale"));
        } else {
            if ("create".equals(PropertyUtils.getSimpleProperty(form, "action"))) {
                // The locale passed in on a create action is actually the parent locale.  Reset the parent
                // locale, increment the type (since we are creating the next type, and clear the locale
                localeType++;
                languageForm.setParentLocale(locale);
                if (localeType == SystemConfigurationUtilities.LOCALE_TYPE_LOCALE) {
                    languageForm.setLocale(locale + "_");
                } else {
                    languageForm.setLocale("");
                }
            }

            String[] sortedKeys = configurationService.getSortedKeys();

            Map<String, String> baseItems = new HashMap<>();
            Map<String, String> langItems = new HashMap<>();
            Map<String, String> locItems = new HashMap<>();
            Map<String, String> items = new HashMap<>();

            log.debug("Loading language elements for edit.  Edit type is " + localeType);

            if (localeType >= SystemConfigurationUtilities.LOCALE_TYPE_BASE) {
                baseItems = configurationService.getDefinedKeys(null);
                //                    putPropertiesKeys(baseItems, ITrackerResources.BASE_LOCALE);
                items = new HashMap<>();
                log.debug("Base Locale has " + baseItems.size() + " keys defined.");
            }

            if (localeType >= SystemConfigurationUtilities.LOCALE_TYPE_LANGUAGE) {
                if (!locale.equalsIgnoreCase(ITrackerResources.BASE_LOCALE)) {
                    String parentLocale = SystemConfigurationUtilities.getLocalePart(locale,
                            SystemConfigurationUtilities.LOCALE_TYPE_LANGUAGE);
                    languageForm.setParentLocale(parentLocale);
                    langItems = configurationService.getDefinedKeys(parentLocale);
                    //                        putPropertiesKeys(langItems, parentLocale);

                    items = new HashMap<>();
                    log.debug("Language " + parentLocale + " has " + langItems.size() + " keys defined.");
                }
            }

            if (localeType == SystemConfigurationUtilities.LOCALE_TYPE_LOCALE) {
                locItems = configurationService.getDefinedKeys(locale);
                //                    putPropertiesKeys(locItems, locale);

                items = locItems;
                log.debug("Locale " + locale + " has " + locItems.size() + " keys defined.");
            }

            if (!"create".equals(PropertyUtils.getSimpleProperty(form, "action"))) {
                Map<String, String> formItems = new HashMap<>();
                for (Enumeration<String> en = ITrackerResources.getBundle(locale).getKeys(); en
                        .hasMoreElements();) {
                    String key = en.nextElement();
                    formItems.put(key, "");
                }
                formItems.putAll(items);

                languageForm.setItems(new TreeMap<>(formItems));
            } else {
                String parentLocale = null;

                if (!locale.equalsIgnoreCase(ITrackerResources.BASE_LOCALE)) {
                    parentLocale = SystemConfigurationUtilities.getLocalePart(locale,
                            SystemConfigurationUtilities.LOCALE_TYPE_LANGUAGE);
                }
                langItems = configurationService.getDefinedKeys(parentLocale);

                Map<String, String> formItems = new HashMap<>();
                if (log.isDebugEnabled()) {
                    log.debug("putPropertiesKeys: items: " + items);
                }
                for (Enumeration<String> en = ITrackerResources.getBundle(locale).getKeys(); en
                        .hasMoreElements();) {
                    String key = en.nextElement();
                    formItems.put(key, "");
                }

                formItems.putAll(items);

                languageForm.setItems(new TreeMap<>(formItems));

            }
            Locale curLocale = ITrackerResources.getLocale(locale);

            Language languageItem = new Language(locale, "itracker.locale.name",
                    ITrackerResources.getString("itracker.locale.name", curLocale));// configurationService.getLanguageItemByKey("itracker.locale.name", curLocale);

            languageForm.setLocaleTitle(languageItem.getResourceValue());
            languageItem = new Language(locale, "itracker.locale.name", ITrackerResources
                    .getString("itracker.locale.name." + locale, ITrackerResources.BASE_LOCALE));// configurationService.getLanguageItemByKey("itracker.locale.name", curLocale);

            languageForm.setLocaleBaseTitle(languageItem.getResourceValue());
            session.setAttribute(Constants.EDIT_LANGUAGE_KEYS_KEY, sortedKeys);
            session.setAttribute(Constants.EDIT_LANGUAGE_BASE_KEY, baseItems);
            session.setAttribute(Constants.EDIT_LANGUAGE_LANG_KEY, langItems);
            session.setAttribute(Constants.EDIT_LANGUAGE_LOC_KEY, locItems);
            session.setAttribute(Constants.EDIT_LANGUAGE_TYPE_KEY, localeType);
            request.setAttribute("languageForm", languageForm);
            if (log.isDebugEnabled()) {
                log.debug("Locale = " + languageForm.getLocale());
            }
            saveToken(request);
            return mapping.getInputForward();
        }
    } catch (RuntimeException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        log.error("Exception while creating edit language form.", e);
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
    }

    if (!errors.isEmpty()) {
        saveErrors(request, errors);
    }

    return mapping.findForward("error");
}