PluginResultManager.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.kernel.bo.service;
017 
018 import java.util.HashMap;
019 import java.util.Iterator;
020 import java.util.Map;
021 
022 import org.qedeq.kernel.bo.module.PluginBo;
023 import org.qedeq.kernel.bo.module.PluginResults;
024 import org.qedeq.kernel.se.common.Plugin;
025 import org.qedeq.kernel.se.common.SourceFileExceptionList;
026 
027 /**
028  * Holds all known QEDEQ modules.
029  */
030 public class PluginResultManager {
031 
032     /** Maps plugins to results. */
033     private final Map plugins = new HashMap();
034 
035 
036     /**
037      * Get all used plugins.
038      *
039      @return  Used plugins.
040      */
041     synchronized PluginBo[] getPlugins() {
042         return (PluginBo[]) plugins.keySet().toArray(new PluginBo[] {});
043     }
044 
045     /**
046      * Clear all plugin results.
047      */
048     public void removeAllResults() {
049         plugins.clear();
050     }
051 
052     /**
053      * Set a plugin execution results.
054      *
055      @param   plugin      Set results for this plugin.
056      @param   errors      Plugin errors.
057      @param   warnings    Plugin warnings.
058      */
059     public synchronized void setResult(final PluginBo plugin, final SourceFileExceptionList errors,
060             final SourceFileExceptionList warnings) {
061         PluginResults results = (PluginResultsplugins.get(plugin);
062         if (results == null) {
063             results = new PluginResults();
064             plugins.put(plugin, results);
065         }
066         results.clear();
067         results.addErrors(errors);
068         results.addWarnings(warnings);
069     }
070 
071     /**
072      * Add results of a plugin.
073      *
074      @param   plugin      Add results for this plugin.
075      @param   errors      Plugin errors.
076      @param   warnings    Plugin warnings.
077      */
078     public synchronized void addResult(final Plugin plugin, final SourceFileExceptionList errors,
079             final SourceFileExceptionList warnings) {
080         PluginResults results = (PluginResultsplugins.get(plugin);
081         if (results == null) {
082             results = new PluginResults();
083             plugins.put(plugin, results);
084         }
085         results.addErrors(errors);
086         results.addWarnings(warnings);
087     }
088 
089     /**
090      * Get all errors that occurred. The resulting object is never <code>null</code>.
091      *
092      @return  Error list.
093      */
094     public SourceFileExceptionList getAllErrors() {
095         final SourceFileExceptionList errors = new SourceFileExceptionList();
096         Iterator iterator = plugins.keySet().iterator();
097         while (iterator.hasNext()) {
098             errors.add(((PluginResultsplugins.get(iterator.next())).getErrors());
099         }
100         return errors;
101     }
102 
103     /**
104      * Get all warnings that occurred. The resulting object is never <code>null</code>.
105      *
106      @return  Warnings list.
107      */
108     public SourceFileExceptionList getAllWarnings() {
109         final SourceFileExceptionList warnings = new SourceFileExceptionList();
110         Iterator iterator = plugins.keySet().iterator();
111         while (iterator.hasNext()) {
112             warnings.add(((PluginResultsplugins.get(iterator.next())).getWarnings());
113         }
114         return warnings;
115     }
116 
117     /**
118      * Get plugin states description.
119      *
120      @return  Textual description of plugin states.
121      */
122     public synchronized String getPluginStateDescription() {
123         final StringBuffer text = new StringBuffer();
124         Iterator iterator = plugins.keySet().iterator();
125         while (iterator.hasNext()) {
126             if (text.length() 0) {
127                 text.append(", ");
128             }
129             final PluginBo key = (PluginBoiterator.next();
130             PluginResults result = (PluginResultsplugins.get(key);
131             text.append(key.getPluginActionName());
132             text.append(" ");
133             if (result.hasErrors() && result.hasWarnings()) {
134                 text.append("has errors and warnings");
135             else if (result.hasErrors()) {
136                 text.append("has errors");
137             else if (result.hasWarnings()) {
138                 text.append("has warnings");
139             else {
140                 text.append("successful");
141             }
142         }
143         return text.toString();
144     }
145 
146 }