Search in sources :

Example 1 with LabelEditorException

use of org.jaffa.applications.jaffa.modules.admin.components.labeleditor.ui.exceptions.LabelEditorException in project jaffa-framework by jaffa-projects.

the class LabelEditorComponent method performSave.

/**
 * This will perform the following tasks.
 * - Add an entry for each label having an override value, to the ApplicationResources.override file.
 * - Remove all entries from the ApplicationResources.override file, for which the override value is blank
 * - Migrate all changes to the ApplicationResources.properties file by invoking InitApp.generateApplicationResources()
 * - Flush the struts properties cache by invoking the flushCache() method on the MessageResources, provided its an instance of 'org.jaffa.util.PropertyMessageResources'
 * @param request The request we are processing.
 * @param messageResources The MessageResources object, which will be flushed, if its an instance of 'org.jaffa.util.PropertyMessageResources'.
 * @throws FrameworkException if any error occurs.
 */
protected void performSave(HttpServletRequest request, MessageResources messageResources) throws FrameworkException {
    String applicationResourcesOverrideLocation = (String) Config.getProperty(Config.PROP_APPLICATION_RESOURCES_OVERRIDE_LOCATION, null);
    try {
        // load the ApplicationResources.override file
        Properties applicationResourcesOverrideProperties = loadPropertiesFromFile(applicationResourcesOverrideLocation, true);
        ApplicationResourceLoader appResourceLoader = ApplicationResourceLoader.getInstance();
        String localeKey = localeKey(LocaleContext.getLocale());
        // Either update or remove a property
        for (Iterator itr = m_labels.keySet().iterator(); itr.hasNext(); ) {
            String label = (String) itr.next();
            Map map = (Map) m_labels.get(label);
            String override = (String) map.get(OVERRIDE);
            if (override != null) {
                // setting the override label into override file
                applicationResourcesOverrideProperties.setProperty(label, override);
                // Applying the changes into ApplicationResources in memory
                appResourceLoader.getLocaleProperties(localeKey).setProperty(label, override);
            } else {
                // removing the override from file if there is any
                applicationResourcesOverrideProperties.remove(label);
                // remove it from memory
                if (ApplicationResourceLoader.getApplicationResourcesManager().getApplicationResourcesLocaleRepository().query(localeKey) != null) {
                    appResourceLoader.getApplicationResourcesOverride(localeKey).remove(label);
                } else {
                    appResourceLoader.getApplicationResourcesOverride(null);
                }
                // reverting/leaving the default value if the override removed.
                appResourceLoader.getLocaleProperties(LocaleContext.getLocale().toString()).setProperty(label, appResourceLoader.getApplicationResourcesDefault().getProperty(label));
            }
        }
        // Sort the  ApplicationResources.override file
        if (applicationResourcesOverrideProperties instanceof ListProperties)
            ((ListProperties) applicationResourcesOverrideProperties).sort();
        // Now save the ApplicationResources.override file into user data directory
        storePropertiesToFile(applicationResourcesOverrideProperties, applicationResourcesOverrideLocation);
        // Flush the struts properties cache by invoking the flushCache() method on the MessageResources, provided its an instance of 'org.jaffa.util.PropertyMessageResources'
        if (messageResources != null && messageResources instanceof PropertyMessageResources) {
            ((PropertyMessageResources) messageResources).flushCache();
            if (log.isDebugEnabled())
                log.debug("Flushed the struts message cache");
        } else {
            if (log.isDebugEnabled())
                log.debug("The struts message cache has not been implemented using the org.jaffa.util.PropertyMessageResources class. Reload the webapp to see the changes to the labels.");
        }
        clearCacheAndRefreshMenu(request);
    } catch (IOException e) {
        String str = "Exception thrown while storing the override values";
        log.error(str, e);
        throw new LabelEditorException(LabelEditorException.WRITE_FAILED, null, e);
    }
}
Also used : ListProperties(org.jaffa.util.ListProperties) PropertyMessageResources(org.jaffa.util.PropertyMessageResources) LabelEditorException(org.jaffa.applications.jaffa.modules.admin.components.labeleditor.ui.exceptions.LabelEditorException) ListProperties(org.jaffa.util.ListProperties) ApplicationResourceLoader(org.jaffa.config.ApplicationResourceLoader)

Example 2 with LabelEditorException

use of org.jaffa.applications.jaffa.modules.admin.components.labeleditor.ui.exceptions.LabelEditorException in project jaffa-framework by jaffa-projects.

the class LabelEditorComponent method retrieveLabels.

/**
 * This retrieves the labels from ApplicationResources.properties and the default and override files.
 * It then populates the 'labels' Map.
 * @throws FrameworkException if any error occurs.
 */
protected void retrieveLabels() throws FrameworkException {
    // clear the widget cache
    getUserSession().getWidgetCache(getComponentId()).clear();
    // The main map, which will contain Map objects keyed by the label
    m_labels = new LinkedHashMap();
    if (getLabelFilter() != null || (getDisplayOverridesOnly() != null && getDisplayOverridesOnly().booleanValue())) {
        try {
            Properties applicationResourcesDefaultProperties = null;
            Properties applicationResourcesOverrideProperties = null;
            String localeKey = localeKey(LocaleContext.getLocale());
            if (ApplicationResourceLoader.getApplicationResourcesManager().getApplicationResourcesLocaleRepository().query(localeKey) != null) {
                applicationResourcesDefaultProperties = ApplicationResourceLoader.getInstance().getApplicationResourcesLocale(localeKey);
                applicationResourcesOverrideProperties = ApplicationResourceLoader.getInstance().getApplicationResourcesOverride(localeKey);
            } else {
                applicationResourcesDefaultProperties = ApplicationResourceLoader.getInstance().getApplicationResourcesDefault();
                applicationResourcesOverrideProperties = ApplicationResourceLoader.getInstance().getApplicationResourcesOverride(null);
            }
            if (applicationResourcesDefaultProperties == null || applicationResourcesDefaultProperties.size() < 1 || applicationResourcesOverrideProperties == null) {
                String str = "There is no default, override properties. The Label Editor component cannot be used";
                log.error(str);
                throw new LabelEditorException(LabelEditorException.CONFIG_ERROR);
            }
            for (Enumeration defaultLabels = applicationResourcesDefaultProperties.propertyNames(); defaultLabels.hasMoreElements(); ) {
                String label = (String) defaultLabels.nextElement();
                // The label should match the labelFilter, if specified
                if (getLabelFilter() != null && !label.matches(getLabelFilter()))
                    continue;
                // The label should be overridden, if the 'displayOverridesOnly' property is true
                if (getDisplayOverridesOnly() != null && getDisplayOverridesOnly().booleanValue() && !applicationResourcesOverrideProperties.containsKey(label))
                    continue;
                final Map<String, String> map = new HashMap();
                map.put(DEFAULT, getEncodingValue(applicationResourcesDefaultProperties, label));
                map.put(OVERRIDE, getEncodingValue(applicationResourcesOverrideProperties, label));
                m_labels.put(label, map);
                // But do that only if a filter is specified and if the 'displayOverridesOnly' property is false
                if (getLabelFilter() != null && (getDisplayOverridesOnly() == null || !getDisplayOverridesOnly().booleanValue()))
                    addInnerTokens(map.get(DEFAULT), m_labels, applicationResourcesDefaultProperties, applicationResourcesOverrideProperties);
            }
        } catch (Exception e) {
            String str = "Exception thrown while reading the ApplicationResources default and override files";
            log.error(str, e);
            throw new LabelEditorException(LabelEditorException.READ_FAILED, null, e);
        }
    }
}
Also used : LabelEditorException(org.jaffa.applications.jaffa.modules.admin.components.labeleditor.ui.exceptions.LabelEditorException) ListProperties(org.jaffa.util.ListProperties) FrameworkException(org.jaffa.exceptions.FrameworkException) LabelEditorException(org.jaffa.applications.jaffa.modules.admin.components.labeleditor.ui.exceptions.LabelEditorException)

Example 3 with LabelEditorException

use of org.jaffa.applications.jaffa.modules.admin.components.labeleditor.ui.exceptions.LabelEditorException in project jaffa-framework by jaffa-projects.

the class LabelEditorComponent method performSyncToSource.

/**
 * This will perform the following tasks.
 * - Migrate all the displayed override values to the appropriate fragment file
 * - Migrate all the displayed override values to the ApplicationResources.default file
 * - Delete all displayed override entries from the ApplicationResources.override file
 * - Migrate all changes to the ApplicationResources.properties file by invoking InitApp.generateApplicationResources()
 * - Flush the struts properties cache by invoking the flushCache() method on the MessageResources, provided its an instance of 'org.jaffa.util.PropertyMessageResources'
 * @param messageResources The MessageResources object, which will be flushed, if its an instance of 'org.jaffa.util.PropertyMessageResources'.
 * @throws FrameworkException if any error occurs.
 */
protected void performSyncToSource(MessageResources messageResources) throws FrameworkException {
    String applicationResourcesDefaultLocation = (String) Config.getProperty(Config.PROP_APPLICATION_RESOURCES_DEFAULT_LOCATION, null);
    String applicationResourcesOverrideLocation = (String) Config.getProperty(Config.PROP_APPLICATION_RESOURCES_OVERRIDE_LOCATION, null);
    try {
        // load the ApplicationResources.default and ApplicationResources.override files
        Properties applicationResourcesDefaultProperties = null;
        Properties applicationResourcesOverrideProperties = null;
        String localeKey = localeKey(LocaleContext.getLocale());
        if (ApplicationResourceLoader.getApplicationResourcesManager().getApplicationResourcesLocaleRepository().query(localeKey) != null) {
            applicationResourcesDefaultProperties = ApplicationResourceLoader.getInstance().getApplicationResourcesLocale(localeKey);
            applicationResourcesOverrideProperties = ApplicationResourceLoader.getInstance().getApplicationResourcesOverride(localeKey);
        } else {
            applicationResourcesDefaultProperties = ApplicationResourceLoader.getInstance().getApplicationResourcesDefault();
            applicationResourcesOverrideProperties = ApplicationResourceLoader.getInstance().getApplicationResourcesOverride(null);
        }
        // Migrate all the displayed override values to the appropriate fragment file
        if (m_searchPathForSourceFragments != null && m_sourceFragmentName != null) {
            FindFiles ff = new FindFiles(m_searchPathForSourceFragments, m_sourceFragmentName);
            List files = ff.getList();
            if (files != null) {
                for (Iterator i = files.iterator(); i.hasNext(); ) {
                    File file = (File) i.next();
                    Properties properties = loadPropertiesFromFile(file.getPath(), true);
                    boolean modified = false;
                    for (Iterator itr = m_labels.keySet().iterator(); itr.hasNext(); ) {
                        String label = (String) itr.next();
                        if (properties.containsKey(label)) {
                            Map map = (Map) m_labels.get(label);
                            String override = (String) map.get(OVERRIDE);
                            if (override != null) {
                                // Migrate to the source file and ApplicationResources.default file
                                // Delete from the ApplicationResources.override file
                                properties.setProperty(label, override);
                                // applicationResourcesDefaultProperties.setProperty(label, override);
                                applicationResourcesOverrideProperties.remove(label);
                                if (log.isInfoEnabled())
                                    log.info(file.getPath() + ": Migrated the override value: " + label + '=' + override);
                                modified = true;
                            }
                        }
                    }
                    // Update the fragment file, maintaining existing comments and order
                    if (modified)
                        storePropertiesToFile(properties, file.getPath());
                }
            }
        }
        // Save the ApplicationResources.default and ApplicationResources.override files
        // storePropertiesToFile(applicationResourcesDefaultProperties, applicationResourcesDefaultLocation);
        storePropertiesToFile(applicationResourcesOverrideProperties, applicationResourcesOverrideLocation);
        // Flush the struts properties cache by invoking the flushCache() method on the MessageResources, provided its an instance of 'org.jaffa.util.PropertyMessageResources'
        if (messageResources != null && messageResources instanceof PropertyMessageResources) {
            ((PropertyMessageResources) messageResources).flushCache();
            if (log.isDebugEnabled())
                log.debug("Flushed the struts message cache");
        } else {
            if (log.isDebugEnabled())
                log.debug("The struts message cache has not been implemented using the org.jaffa.util.PropertyMessageResources class. Reload the webapp to see the changes to the labels.");
        }
    } catch (IOException e) {
        String str = "Sync to Source failed";
        log.error(str, e);
        throw new LabelEditorException(LabelEditorException.SYNC_TO_SOURCE_FAILED, null, e);
    }
}
Also used : FindFiles(org.jaffa.util.FindFiles) PropertyMessageResources(org.jaffa.util.PropertyMessageResources) LabelEditorException(org.jaffa.applications.jaffa.modules.admin.components.labeleditor.ui.exceptions.LabelEditorException) ListProperties(org.jaffa.util.ListProperties)

Aggregations

LabelEditorException (org.jaffa.applications.jaffa.modules.admin.components.labeleditor.ui.exceptions.LabelEditorException)3 ListProperties (org.jaffa.util.ListProperties)3 PropertyMessageResources (org.jaffa.util.PropertyMessageResources)2 ApplicationResourceLoader (org.jaffa.config.ApplicationResourceLoader)1 FrameworkException (org.jaffa.exceptions.FrameworkException)1 FindFiles (org.jaffa.util.FindFiles)1