]> SALOME platform Git repositories - tools/siman.git/blobdiff - Workspace/Siman/src/org/splat/simer/ApplicationSettings.java
Salome HOME
Modifications done to respect PMD rules. Versioning a document is fixed. Validation...
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / ApplicationSettings.java
index 9082d0e5639126fbab93445eafc6e649a6d260e9..427925a50e3d65705ea8590ca64432f67e49d6d6 100644 (file)
@@ -2,6 +2,7 @@ package org.splat.simer;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -11,42 +12,40 @@ import java.util.Locale;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
-import java.util.Vector;
 
 import javax.servlet.ServletContext;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-import org.w3c.dom.NamedNodeMap;
 
 import org.apache.log4j.Logger;
 import org.splat.dal.bo.kernel.User;
-import org.splat.manox.XDOM;
 import org.splat.dal.bo.som.Document;
-import org.splat.som.DocumentRights;
 import org.splat.dal.bo.som.DocumentType;
 import org.splat.dal.bo.som.KnowledgeElement;
 import org.splat.dal.bo.som.ProgressState;
+import org.splat.dal.bo.som.SimulationContext;
+import org.splat.manox.XDOM;
 import org.splat.service.DocumentTypeService;
 import org.splat.service.technical.ProjectSettingsService;
-import org.splat.simer.Converter;
-import org.splat.dal.bo.som.SimulationContext;
+import org.splat.som.DocumentRights;
 import org.splat.som.Step;
 import org.splat.som.StudyRights;
 import org.splat.wapp.MenuItem;
-import org.splat.wapp.PopupMenu;
 import org.splat.wapp.PopupItem;
+import org.splat.wapp.PopupMenu;
 import org.splat.wapp.SimpleMenu;
 import org.splat.wapp.ToolBar;
 import org.springframework.web.context.ServletContextAware;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 
 public class ApplicationSettings implements ServletContextAware {
 
        /**
         * Application settings logger.
         */
-       protected final static Logger logger = Logger
+       protected final static Logger LOG = Logger
                        .getLogger(ApplicationSettings.class);
 
        /**
@@ -56,9 +55,9 @@ public class ApplicationSettings implements ServletContextAware {
        
        private String wappserver;
        private String wappname;
-       private Properties wapprops = new Properties(); // General properties from the application properties files
+       private final Properties wapprops = new Properties(); // General properties from the application properties files
        private Locale locale; // Current user locale
-       private Map<String, SimpleMenu> menus = new HashMap<String, SimpleMenu>(); // Application menus
+       private final Map<String, SimpleMenu> menus = new HashMap<String, SimpleMenu>(); // Application menus
        private Map<Integer, ToolBar> bars = null; // Study module-bars structured by steps
        private Map<String, PopupMenu> popups = null;
        private Map<String, Map<String, Object>> filter = null; // Named search filters
@@ -66,9 +65,9 @@ public class ApplicationSettings implements ServletContextAware {
        private Map<String, String> tempfile = null; // Available template files
        private String[] viewermap = null; // List of file extensions mapped to a viewer
        private Map<String, Converter> convertmap = null; // Available document format converters
-       private Properties jndprops = new Properties(); // JNDI context for launching converters
+       private final Properties jndprops = new Properties(); // JNDI context for launching converters
 
-       private static ApplicationSettings my = null; // Singleton instance
+       private static ApplicationSettings my = new ApplicationSettings(); // Singleton instance
        /**
         * Injected project settings service.
         */
@@ -93,7 +92,7 @@ public class ApplicationSettings implements ServletContextAware {
         * @param projectSettingsService
         *            the projectSettingsService to set
         */
-       public void setProjectSettings(ProjectSettingsService projectSettingsService) {
+       public void setProjectSettings(final ProjectSettingsService projectSettingsService) {
                _projectSettingsService = projectSettingsService;
        }
 
@@ -202,34 +201,44 @@ public class ApplicationSettings implements ServletContextAware {
                                        .confirmation("message.delete.study"));
                }
 
-               public boolean isEnabled(String name) {
-                       if (user == null)
+               @Override
+               public boolean isEnabled(final String name) {
+                       if (user == null) {
                                return false; // Should not happen
+                       }
                        Item item = Item.valueOf(name);
-                       if (item == Item.publish)
+                       if (item == Item.publish) {
                                return user.canPublish();
-                       if (item == Item.edit)
+                       }
+                       if (item == Item.edit) {
                                return user.canEditProperties();
-                       if (item == Item.script)
+                       }
+                       if (item == Item.script) {
                                return user.canAddScenario();
-                       if (item == Item.version)
+                       }
+                       if (item == Item.version) {
                                return user.canVersion();
-                       if (item == Item.remove)
+                       }
+                       if (item == Item.remove) {
                                return user.canRemove();
-                       if (item == Item.purge)
+                       }
+                       if (item == Item.purge) {
                                return user.canPurge();
+                       }
                        return false;
                }
 
-               public void setContext(String name, Object context) {
+               @Override
+               public void setContext(final String name, final Object context) {
                        if (context instanceof StudyRights) {
                                user = (StudyRights) context; // Just for optimizing
                                boolean history = user.getOperand().isVersioned();
                                PopupItem item = this.item("remove");
-                               if (history)
+                               if (history) {
                                        item.rename("menu.remove.version");
-                               else
+                               } else {
                                        item.rename("menu.remove.study");
+                               }
                        }
                }
        }
@@ -266,38 +275,50 @@ public class ApplicationSettings implements ServletContextAware {
                                        "message.delete.document"));
                }
 
-               public boolean isEnabled(String name) {
-                       if (user == null)
+               @Override
+               public boolean isEnabled(final String name) {
+                       if (user == null) {
                                return false; // Should not happen
+                       }
                        Item item = Item.valueOf(name);
-                       if (item == Item.accept)
+                       if (item == Item.accept) {
                                return user.canAccept();
-                       if (item == Item.promote)
+                       }
+                       if (item == Item.promote) {
                                return user.canPromote();
-                       if (item == Item.rename)
+                       }
+                       if (item == Item.rename) {
                                return user.canRename();
-                       if (item == Item.attach)
+                       }
+                       if (item == Item.attach) {
                                return user.canAttach();
-                       if (item == Item.version)
+                       }
+                       if (item == Item.version) {
                                return user.canVersion();
-                       if (item == Item.replace)
+                       }
+                       if (item == Item.replace) {
                                return user.canReplace();
-                       if (item == Item.purge)
+                       }
+                       if (item == Item.purge) {
                                return user.canPurge();
-                       if (item == Item.remove)
+                       }
+                       if (item == Item.remove) {
                                return user.canRemove();
+                       }
                        return false;
                }
 
-               public void setContext(String name, Object context) {
+               @Override
+               public void setContext(final String name, final Object context) {
                        if (context instanceof DocumentRights) {
                                user = (DocumentRights) context; // Just for optimizing
                                Document downer = user.getOperand();
                                PopupItem item = this.item("remove");
-                               if (downer.isVersioned())
+                               if (downer.isVersioned()) {
                                        item.rename("menu.remove.version");
-                               else
+                               } else {
                                        item.rename("menu.remove.document");
+                               }
                        }
                }
        }
@@ -325,24 +346,32 @@ public class ApplicationSettings implements ServletContextAware {
                                        "notyetimplemented").confirmation("message.purge.document"));
                }
 
-               public boolean isEnabled(String name) {
-                       if (user == null)
+               @Override
+               public boolean isEnabled(final String name) {
+                       if (user == null) {
                                return false; // Should not happen
+                       }
                        Item item = Item.valueOf(name);
-                       if (item == Item.demote)
+                       if (item == Item.demote) {
                                return user.canDemote();
-                       if (item == Item.promote)
+                       }
+                       if (item == Item.promote) {
                                return user.canReview();
-                       if (item == Item.attach)
+                       }
+                       if (item == Item.attach) {
                                return user.canAttach();
-                       if (item == Item.version)
+                       }
+                       if (item == Item.version) {
                                return user.canVersion();
-                       if (item == Item.purge)
+                       }
+                       if (item == Item.purge) {
                                return user.canPurge();
+                       }
                        return false;
                }
 
-               public void setContext(String name, Object context) {
+               @Override
+               public void setContext(final String name, final Object context) {
                        if (context instanceof DocumentRights) {
                                user = (DocumentRights) context; // Just for optimizing
                        }
@@ -351,7 +380,7 @@ public class ApplicationSettings implements ServletContextAware {
 
        private static class NotResultDocumentPopup extends PopupMenu {
                // ----------------------------------------------------------------
-               private DocumentRights user = null;
+               private final DocumentRights user = null;
 
                private NotResultDocumentPopup() {
                        addItem("demote", new PopupItem("menu.demote").icon(
@@ -390,20 +419,26 @@ public class ApplicationSettings implements ServletContextAware {
                                        .confirmation("message.approve.document"));
                }
 
-               public boolean isEnabled(String name) {
-                       if (user == null)
+               @Override
+               public boolean isEnabled(final String name) {
+                       if (user == null) {
                                return false; // Should not happen
+                       }
                        Item item = Item.valueOf(name);
-                       if (item == Item.undo)
+                       if (item == Item.undo) {
                                return user.canInvalidate();
-                       if (item == Item.demote)
+                       }
+                       if (item == Item.demote) {
                                return user.canDemote();
-                       if (item == Item.approve)
+                       }
+                       if (item == Item.approve) {
                                return user.canApprove();
+                       }
                        return false;
                }
 
-               public void setContext(String name, Object context) {
+               @Override
+               public void setContext(final String name, final Object context) {
                        if (context instanceof DocumentRights) {
                                user = (DocumentRights) context; // Just for optimizing
                        }
@@ -438,20 +473,26 @@ public class ApplicationSettings implements ServletContextAware {
                                        "message.delete.document"));
                }
 
-               public boolean isEnabled(String name) {
-                       if (user == null)
+               @Override
+               public boolean isEnabled(final String name) {
+                       if (user == null) {
                                return false; // Should not happen
+                       }
                        Item item = Item.valueOf(name);
-                       if (item == Item.rename)
+                       if (item == Item.rename) {
                                return user.canRename();
-                       if (item == Item.replace)
+                       }
+                       if (item == Item.replace) {
                                return user.canReplace();
-                       if (item == Item.remove)
+                       }
+                       if (item == Item.remove) {
                                return user.canRemove();
+                       }
                        return false;
                }
 
-               public void setContext(String name, Object context) {
+               @Override
+               public void setContext(final String name, final Object context) {
                        if (context instanceof DocumentRights) {
                                user = (DocumentRights) context; // Just for optimizing
                        }
@@ -474,7 +515,8 @@ public class ApplicationSettings implements ServletContextAware {
                                        "message.delete.context"));
                }
 
-               public boolean isEnabled(String name) {
+               @Override
+               public boolean isEnabled(final String name) {
                        Item item = Item.valueOf(name);
 
                        if (item == Item.rename) {
@@ -486,7 +528,8 @@ public class ApplicationSettings implements ServletContextAware {
                        return true;
                }
 
-               public void setContext(String name, Object context) {
+               @Override
+               public void setContext(final String name, final Object context) {
                        if (context instanceof SimulationContextFacade) {
                                owner = (SimulationContextFacade) context; // Just for optimizing
                        } else {
@@ -518,23 +561,27 @@ public class ApplicationSettings implements ServletContextAware {
                                        "message.delete.knowledge"));
                }
 
-               public boolean isEnabled(String name) {
+               @Override
+               public boolean isEnabled(final String name) {
                        Item item = Item.valueOf(name);
 
                        if (item == Item.promote) {
-                               if (owner.getProgressState() != ProgressState.inDRAFT)
+                               if (owner.getProgressState() != ProgressState.inDRAFT) {
                                        return false;
+                               }
                        } else if (item == Item.demote) {
-                               if (owner.getProgressState() != ProgressState.inCHECK)
+                               if (owner.getProgressState() != ProgressState.inCHECK) {
                                        return false;
+                               }
                        }
                        return true;
                }
 
-               public void setContext(String name, Object context) {
-                       if (context instanceof KnowledgeElement)
+               @Override
+               public void setContext(final String name, final Object context) {
+                       if (context instanceof KnowledgeElement) {
                                owner = (KnowledgeElement) context; // Just for optimizing
-                       else {
+                       else {
                                super.setContext(name, context);
                        }
                }
@@ -545,10 +592,6 @@ public class ApplicationSettings implements ServletContextAware {
        // ==============================================================================================================================
 
        public static ApplicationSettings getMe() {
-               // ------------------------------------------
-               if (my == null) {
-                       my = new ApplicationSettings();
-               }
                return my; // The application is supposed being previously created below
        }
 
@@ -557,7 +600,7 @@ public class ApplicationSettings implements ServletContextAware {
         * @param lang
         * @return
         */
-       public ApplicationSettings init(String wappurl, Locale lang)
+       public ApplicationSettings init(final String wappurl, final Locale lang)
                        throws IOException {
                ClassLoader cloader = getClass().getClassLoader();
                String[] wurl = wappurl.split("/"); // [0]="http:", [1]="", [2]="{server}:{port}", [3]="name"
@@ -570,7 +613,7 @@ public class ApplicationSettings implements ServletContextAware {
                wapprops.load(cloader.getResourceAsStream(wappname + ".properties"));
                jndprops.load(cloader.getResourceAsStream("jndi.properties"));
 
-               logger.info("Application root set to "
+               LOG.info("Application root set to "
                                + wapprops.getProperty("wapp.root"));
                if (my == null) {
                        my = this;
@@ -584,7 +627,7 @@ public class ApplicationSettings implements ServletContextAware {
        // Public member functions
        // ==============================================================================================================================
 
-       public void configure(String filename) {
+       public void configure(final String filename) {
                // ---------------------------------------
                // Non customizable settings
                menus.clear();
@@ -619,7 +662,7 @@ public class ApplicationSettings implements ServletContextAware {
                fprop.put("author", "0");
                fprop.put("reference", "");
                fprop.put("title", "");
-               fprop.put("context", new Vector<SimulationContext>());
+               fprop.put("context", new ArrayList<SimulationContext>());
 
                Map<String, Object> gprop = new HashMap<String, Object>();
                gprop.put("visibility", "PUBLIC");
@@ -629,7 +672,7 @@ public class ApplicationSettings implements ServletContextAware {
                gprop.put("author", "0");
                gprop.put("reference", "");
                gprop.put("title", "");
-               gprop.put("context", new Vector<SimulationContext>());
+               gprop.put("context", new ArrayList<SimulationContext>());
 
                defdoctype = new LinkedHashMap<String, DocumentType>();
                tempfile = new HashMap<String, String>();
@@ -644,7 +687,7 @@ public class ApplicationSettings implements ServletContextAware {
                if (config.exists()) {
                        loadCustomization(config); // Sets default document types, installed modules and available templates
                } else {
-                       logger.info("Could not find the application configuration file \""
+                       LOG.info("Could not find the application configuration file \""
                                        + config.getAbsolutePath() + "\", using default settings");
                }
                // Settings based on the customization
@@ -656,18 +699,21 @@ public class ApplicationSettings implements ServletContextAware {
                                .hasNext();) {
                        ProjectSettingsService.Step step = i.next();
                        List<String> formats = getDefaultFormats(step);
-                       if (formats.size() == 0)
+                       if (formats.size() == 0) {
                                continue;
+                       }
 
                        ToolBar bar = new ToolBar(24); // Height of the module-bar
                        HashSet<String> module = new HashSet<String>(); // For not duplicating modules
                        for (Iterator<String> j = formats.iterator(); j.hasNext();) {
                                String format = j.next();
                                String command = getApplicationProperty("executable." + format);
-                               if (command == null)
+                               if (command == null) {
                                        continue; // Module not installed
-                               if (module.contains(command))
+                               }
+                               if (module.contains(command)) {
                                        continue;
+                               }
                                module.add(command);
                                String[] parsed = command.split("/");
                                String[] name = parsed[parsed.length - 1].split("\\x2E");
@@ -689,8 +735,9 @@ public class ApplicationSettings implements ServletContextAware {
                                        File image = new File(ApplicationSettings
                                                        .getApplicationSkinPath()
                                                        + icon);
-                                       if (!image.exists())
+                                       if (!image.exists()) {
                                                icon = "tool.any.png";
+                                       }
                                        bar.addTool(tool, icon, command);
                                } else {
                                        docname = "/jsp/newDocument.jsp?type=" + docname;
@@ -698,17 +745,19 @@ public class ApplicationSettings implements ServletContextAware {
                                        File image = new File(ApplicationSettings
                                                        .getApplicationSkinPath()
                                                        + icon);
-                                       if (!image.exists())
+                                       if (!image.exists()) {
                                                icon = "tool.any.png";
+                                       }
                                        bar.addTool(name[0], icon, command, docname);
                                }
                        }
-                       if (!bar.isEmpty())
+                       if (!bar.isEmpty()) {
                                bars.put(step.getNumber(), bar);
+                       }
                }
        }
 
-       public String getApplicationProperty(String name) {
+       public String getApplicationProperty(final String name) {
                return wapprops.getProperty(name); // May be null
        }
 
@@ -724,12 +773,12 @@ public class ApplicationSettings implements ServletContextAware {
                return url.toString();
        }
 
-       public Map<String, Object> getFilter(String name) {
+       public Map<String, Object> getFilter(final String name) {
                // --------------------------------------------------
                return filter.get(name);
        }
 
-       public ToolBar getModuleBar(Step step) {
+       public ToolBar getModuleBar(final Step step) {
                // -----------------------------------------
                return bars.get(step.getNumber());
        }
@@ -758,19 +807,19 @@ public class ApplicationSettings implements ServletContextAware {
                return my.getApplicationRootPath() + "skin/";
        }
 
-       public static Converter getConverter(DocumentType type, String format) {
+       public static Converter getConverter(final DocumentType type, final String format) {
                // -----------------------------------------------------------------------
                return my.convertmap.get(format + type.getName()); // May be null;
        }
 
-       public static DocumentType getDefaultDocumentType(Step step, String format) {
+       public static DocumentType getDefaultDocumentType(final Step step, final String format) {
                // ----------------------------------------------------------------------------
                String[] table = format.split("\\x2E");
                return my.defdoctype.get(step.getNumber() + "."
                                + table[table.length - 1]); // May be null
        }
 
-       public static String getDownloadURL(User user) {
+       public static String getDownloadURL(final User user) {
                // --------------------------------------------------
                StringBuffer url = new StringBuffer("http://").append(my.wappserver)
                                .append("/download/").append(user.getUsername()).append("/");
@@ -782,12 +831,12 @@ public class ApplicationSettings implements ServletContextAware {
                return my.locale;
        }
 
-       public static SimpleMenu getMenu(String name) {
+       public static SimpleMenu getMenu(final String name) {
                // ----------------------------------------------
                return my.menus.get(name);
        }
 
-       public static PopupMenu getPopupMenu(String name) {
+       public static PopupMenu getPopupMenu(final String name) {
                // --------------------------------------------------
                return my.popups.get(name);
        }
@@ -803,8 +852,9 @@ public class ApplicationSettings implements ServletContextAware {
                // ---------------------------------------------
                String[] code = my.wapprops.getProperty("locale.supported").split(",");
                Locale[] result = new Locale[code.length];
-               for (int i = 0; i < code.length; i++)
+               for (int i = 0; i < code.length; i++) {
                        result[i] = new Locale(code[i]);
+               }
                return result;
        }
 
@@ -827,24 +877,26 @@ public class ApplicationSettings implements ServletContextAware {
        // Private services
        // ==============================================================================================================================
 
-       private List<String> getDefaultFormats(ProjectSettingsService.Step step) {
+       private List<String> getDefaultFormats(final ProjectSettingsService.Step step) {
                // ------------------------------------------------------------------
                Set<String> keys = defdoctype.keySet();
                int number = step.getNumber();
-               Vector<String> result = new Vector<String>();
+               List<String> result = new ArrayList<String>();
 
                for (Iterator<String> i = keys.iterator(); i.hasNext();) {
                        String[] key = i.next().split("\\x2E");
-                       if (Integer.valueOf(key[0]) != number)
+                       if (Integer.valueOf(key[0]) != number) {
                                continue;
-                       if (key[1].equals("pdf"))
+                       }
+                       if (key[1].equals("pdf")) {
                                continue; // PDF is not an authoring format
+                       }
                        result.add(key[1]); // Formats are unique
                }
                return result;
        }
 
-       private void loadCustomization(File config) {
+       private void loadCustomization(final File config) {
                // --------------------------------------------
                try {
                        DocumentBuilderFactory dfactory = javax.xml.parsers.DocumentBuilderFactory
@@ -868,16 +920,18 @@ public class ApplicationSettings implements ServletContextAware {
                        }
                        for (int i = 0; i < nlist.getLength(); i++) {
                                child = nlist.item(i);
-                               if (!child.getNodeName().equals("step"))
+                               if (!child.getNodeName().equals("step")) {
                                        continue;
+                               }
 
                                String nstep = child.getAttributes().getNamedItem("number")
                                                .getNodeValue();
                                NodeList map = child.getChildNodes();
                                for (int j = 0; j < map.getLength(); j++) {
                                        child = map.item(j);
-                                       if (!child.getNodeName().equals("mapping"))
+                                       if (!child.getNodeName().equals("mapping")) {
                                                continue;
+                                       }
                                        NamedNodeMap natr = child.getAttributes();
                                        String dext = natr.getNamedItem("extension").getNodeValue();
                                        String type = natr.getNamedItem("type").getNodeValue();
@@ -889,8 +943,9 @@ public class ApplicationSettings implements ServletContextAware {
                        nlist = child.getChildNodes();
                        for (int i = 0; i < nlist.getLength(); i++) {
                                child = nlist.item(i);
-                               if (!child.getNodeName().equals("mapping"))
+                               if (!child.getNodeName().equals("mapping")) {
                                        continue;
+                               }
 
                                NamedNodeMap natr = child.getAttributes();
                                String dext = natr.getNamedItem("extension").getNodeValue();
@@ -924,8 +979,9 @@ public class ApplicationSettings implements ServletContextAware {
                        nlist = child.getChildNodes();
                        for (int i = 0; i < nlist.getLength(); i++) {
                                child = nlist.item(i);
-                               if (!child.getNodeName().equals("document"))
+                               if (!child.getNodeName().equals("document")) {
                                        continue;
+                               }
 
                                NamedNodeMap natr = child.getAttributes();
                                String type = natr.getNamedItem("type").getNodeValue();
@@ -933,7 +989,7 @@ public class ApplicationSettings implements ServletContextAware {
                                tempfile.put(type, file);
                        }
                } catch (Exception error) {
-                       logger.info("Error in customization", error);
+                       LOG.info("Error in customization", error);
                }
        }
 
@@ -952,7 +1008,7 @@ public class ApplicationSettings implements ServletContextAware {
         * @param documentTypeService
         *            the documentTypeService to set
         */
-       public void setDocumentTypeService(DocumentTypeService documentTypeService) {
+       public void setDocumentTypeService(final DocumentTypeService documentTypeService) {
                _documentTypeService = documentTypeService;
        }
 
@@ -961,7 +1017,7 @@ public class ApplicationSettings implements ServletContextAware {
         * @see org.apache.struts2.util.ServletContextAware#setServletContext(javax.servlet.ServletContext)
         */
        @Override
-       public void setServletContext(ServletContext context) {
+       public void setServletContext(final ServletContext context) {
                _servletContext = context;
        }