Search in sources :

Example 1 with ListProperties

use of org.jaffa.util.ListProperties 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 ListProperties

use of org.jaffa.util.ListProperties in project jaffa-framework by jaffa-projects.

the class LabelEditorComponent method loadPropertiesFromFile.

/**
 * Reads the input file and loads its properties into a Properties object.
 * @param fileName The file to be loaded.
 * @param maintainFileFormat If true, then the comments and order for each property will be maintained.
 * @throws IOException if any error occurs.
 * @return a Properties object loaded with the properties from the input file.
 */
protected static Properties loadPropertiesFromFile(String fileName, boolean maintainFileFormat) throws IOException {
    InputStreamReader is = null;
    Properties properties;
    if (maintainFileFormat)
        properties = new ListProperties();
    else
        properties = new Properties();
    try {
        final File file = new File(fileName);
        if (file.exists()) {
            is = new InputStreamReader(new FileInputStream(fileName), "UTF-8");
            properties.load(is);
            if (log.isDebugEnabled())
                log.debug("Loaded properties from " + fileName);
        } else {
            if (log.isDebugEnabled())
                log.debug("No properties loaded, since file does not exist " + fileName);
        }
        return properties;
    } finally {
        if (is != null)
            is.close();
    }
}
Also used : ListProperties(org.jaffa.util.ListProperties) ListProperties(org.jaffa.util.ListProperties)

Example 3 with ListProperties

use of org.jaffa.util.ListProperties in project jaffa-framework by jaffa-projects.

the class PropertyEditorHelper method getPropertiesWM.

/**
 * Convert a property string into a grid model, if the model is already
 * being cached on the form, it returns the cached version. If it was not cached
 * it is added to the cache after it has been create.
 * <p>
 * <b>NOTE: if the model is cached and this method is called with a 'new' source
 * string, it will only return the current cached version, not a new grid based
 * on the new source string. If you want this, clear the model cache prior to calling
 * this method
 * </b>
 * @param form the form object that will contains this model
 * @param cacheName the name being used by the form to cache this model
 * @param source the source string containing the property data
 * @return the converted grid model
 */
public static GridModel getPropertiesWM(FormBase form, String cacheName, String source) {
    GridModel model = (GridModel) form.getWidgetCache().getModel(cacheName);
    if (model == null) {
        model = new GridModel();
        ListProperties p = new ListProperties();
        try {
            if (source != null)
                p.load(new ByteArrayInputStream(source.getBytes()));
            for (Enumeration e = p.keys(); e.hasMoreElements(); ) {
                String key = (String) e.nextElement();
                String value = (String) p.getProperty(key);
                String comment = (String) p.getComments(key);
                GridModelRow row = model.newRow();
                row.addElement(KEY, new EditBoxModel(key == null ? EMPTY : key));
                row.addElement(VALUE, new EditBoxModel(value == null ? EMPTY : value));
                row.addElement(COMMENT, new EditBoxModel(comment == null ? EMPTY : value));
            }
            form.getWidgetCache().addModel(cacheName, model);
        } catch (IOException e) {
            log.error("Failed to Load Properties For Field : " + cacheName, e);
        }
    }
    return model;
}
Also used : ListProperties(org.jaffa.util.ListProperties) Enumeration(java.util.Enumeration) ByteArrayInputStream(java.io.ByteArrayInputStream) GridModel(org.jaffa.presentation.portlet.widgets.model.GridModel) EditBoxModel(org.jaffa.presentation.portlet.widgets.model.EditBoxModel) IOException(java.io.IOException) GridModelRow(org.jaffa.presentation.portlet.widgets.model.GridModelRow)

Example 4 with ListProperties

use of org.jaffa.util.ListProperties in project jaffa-framework by jaffa-projects.

the class PropertyEditorHelper method getProperties.

/**
 * Convert the user grid back to a string format
 * @param model User grid model that contains the property entries
 * @return the formated string, including a date time stamp.
 * Returns a null string if there are no rows.
 */
public static String getProperties(GridModel model) {
    if (model == null || model.getRows() == null || model.getRows().size() == 0)
        return null;
    ListProperties p = new ListProperties();
    for (Iterator i = model.getRows().iterator(); i.hasNext(); ) {
        GridModelRow row = (GridModelRow) i.next();
        EditBoxModel e = (EditBoxModel) row.get(KEY);
        String key = (String) (e == null ? null : e.getValue());
        e = (EditBoxModel) row.get(VALUE);
        String value = (String) (e == null ? null : e.getValue());
        e = (EditBoxModel) row.get(COMMENT);
        String comment = (String) (e == null ? null : e.getValue());
        if ((key != null && key.length() > 0) || (value != null && value.length() > 0)) {
            log.debug("Save Prop '" + (key == null ? "null" : key + "[" + key.length() + "]") + "=" + (value == null ? "null" : value + "[" + value.length() + "]") + "'");
            p.setProperty(key, value, comment);
        }
    }
    try {
        ByteArrayOutputStream b = new ByteArrayOutputStream();
        p.store(b, null);
        b.close();
        return b.toString();
    } catch (IOException e) {
        log.error("Failed to Store Field Properties", e);
        return null;
    }
}
Also used : ListProperties(org.jaffa.util.ListProperties) Iterator(java.util.Iterator) EditBoxModel(org.jaffa.presentation.portlet.widgets.model.EditBoxModel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) GridModelRow(org.jaffa.presentation.portlet.widgets.model.GridModelRow)

Example 5 with ListProperties

use of org.jaffa.util.ListProperties in project jaffa-framework by jaffa-projects.

the class LabelHelper 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 static void performSave(Map labels) 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();
        // Either update or remove a property
        for (Iterator itr = labels.keySet().iterator(); itr.hasNext(); ) {
            String label = (String) itr.next();
            Map map = (Map) 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(ApplicationResourceLoader.DEFAULT_PROP_LOCALE_KEY).setProperty(label, override);
            } else {
                // removing the override from file if there is any
                applicationResourcesOverrideProperties.remove(label);
                // reverting/leaving the default value if the override removed.
                appResourceLoader.getLocaleProperties(ApplicationResourceLoader.DEFAULT_PROP_LOCALE_KEY).setProperty(label, appResourceLoader.getApplicationResourcesDefault().getProperty(label));
            }
        }
        // Sort the  ApplicationResources.override file
        if (applicationResourcesOverrideProperties instanceof ListProperties)
            ((ListProperties) applicationResourcesOverrideProperties).sort();
        // Now save the ApplicationResources.override file
        storePropertiesToFile(applicationResourcesOverrideProperties, applicationResourcesOverrideLocation);
        // Migrate all changes to the ApplicationResources.properties file by invoking InitApp.generateApplicationResources()
        // InitApp.generateApplicationResources();
        ((PropertyMessageResources) PropertyMessageResourcesFactory.getDefaultMessageResources()).flushCache();
        if (log.isDebugEnabled())
            log.debug("Flushed the struts message cache");
    } catch (IOException e) {
        throw new LabelException(LabelException.WRITE_ERROR);
    }
}
Also used : ListProperties(org.jaffa.util.ListProperties) PropertyMessageResources(org.jaffa.util.PropertyMessageResources) Iterator(java.util.Iterator) IOException(java.io.IOException) ListProperties(org.jaffa.util.ListProperties) Properties(java.util.Properties) HashMap(java.util.HashMap) Map(java.util.Map) LabelException(org.jaffa.exceptions.LabelException) ApplicationResourceLoader(org.jaffa.config.ApplicationResourceLoader)

Aggregations

ListProperties (org.jaffa.util.ListProperties)6 IOException (java.io.IOException)3 Iterator (java.util.Iterator)2 Properties (java.util.Properties)2 ApplicationResourceLoader (org.jaffa.config.ApplicationResourceLoader)2 EditBoxModel (org.jaffa.presentation.portlet.widgets.model.EditBoxModel)2 GridModelRow (org.jaffa.presentation.portlet.widgets.model.GridModelRow)2 PropertyMessageResources (org.jaffa.util.PropertyMessageResources)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 LabelEditorException (org.jaffa.applications.jaffa.modules.admin.components.labeleditor.ui.exceptions.LabelEditorException)1 LabelException (org.jaffa.exceptions.LabelException)1 GridModel (org.jaffa.presentation.portlet.widgets.model.GridModel)1