PluginResultManager.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2011,  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.DefaultSourceFileExceptionList;
025 import org.qedeq.kernel.se.common.Plugin;
026 import org.qedeq.kernel.se.common.SourceFileExceptionList;
027 
028 /**
029  * Holds all known QEDEQ modules.
030  */
031 public class PluginResultManager {
032 
033     /** Maps plugins to results. */
034     private final Map plugins = new HashMap();
035 
036 
037     /**
038      * Get all used plugins.
039      *
040      @return  Used plugins.
041      */
042     synchronized PluginBo[] getPlugins() {
043         return (PluginBo[]) plugins.keySet().toArray(new PluginBo[] {});
044     }
045 
046     /**
047      * Clear all plugin results.
048      */
049     public void removeAllResults() {
050         plugins.clear();
051     }
052 
053     /**
054      * Set a plugin execution results.
055      *
056      @param   plugin      Set results for this plugin.
057      @param   errors      Plugin errors.
058      @param   warnings    Plugin warnings.
059      */
060     public synchronized void setResult(final PluginBo plugin, final SourceFileExceptionList errors,
061             final SourceFileExceptionList warnings) {
062         PluginResults results = (PluginResultsplugins.get(plugin);
063         if (results == null) {
064             results = new PluginResults();
065             plugins.put(plugin, results);
066         }
067         results.clear();
068         results.addErrors(errors);
069         results.addWarnings(warnings);
070     }
071 
072     /**
073      * Add results of a plugin.
074      *
075      @param   plugin      Add results for this plugin.
076      @param   errors      Plugin errors.
077      @param   warnings    Plugin warnings.
078      */
079     public synchronized void addResult(final Plugin plugin, final SourceFileExceptionList errors,
080             final SourceFileExceptionList warnings) {
081         PluginResults results = (PluginResultsplugins.get(plugin);
082         if (results == null) {
083             results = new PluginResults();
084             plugins.put(plugin, results);
085         }
086         results.addErrors(errors);
087         results.addWarnings(warnings);
088     }
089 
090     /**
091      * Get all errors that occurred. The resulting object is never <code>null</code>.
092      *
093      @return  Error list.
094      */
095     public SourceFileExceptionList getAllErrors() {
096         final DefaultSourceFileExceptionList errors = new DefaultSourceFileExceptionList();
097         Iterator iterator = plugins.keySet().iterator();
098         while (iterator.hasNext()) {
099             errors.add(((PluginResultsplugins.get(iterator.next())).getErrors());
100         }
101         return errors;
102     }
103 
104     /**
105      * Get all warnings that occurred. The resulting object is never <code>null</code>.
106      *
107      @return  Warnings list.
108      */
109     public SourceFileExceptionList getAllWarnings() {
110         final DefaultSourceFileExceptionList warnings = new DefaultSourceFileExceptionList();
111         Iterator iterator = plugins.keySet().iterator();
112         while (iterator.hasNext()) {
113             warnings.add(((PluginResultsplugins.get(iterator.next())).getWarnings());
114         }
115         return warnings;
116     }
117 
118     /**
119      * Get plugin states description.
120      *
121      @return  Textual description of plugin states.
122      */
123     public synchronized String getPluginStateDescription() {
124         final StringBuffer text = new StringBuffer();
125         Iterator iterator = plugins.keySet().iterator();
126         while (iterator.hasNext()) {
127             if (text.length() 0) {
128                 text.append(", ");
129             }
130             final PluginBo key = (PluginBoiterator.next();
131             PluginResults result = (PluginResultsplugins.get(key);
132             text.append(key.getPluginActionName());
133             text.append(" ");
134             if (result.hasErrors() && result.hasWarnings()) {
135                 text.append("has errors and warnings");
136             else if (result.hasErrors()) {
137                 text.append("has errors");
138             else if (result.hasWarnings()) {
139                 text.append("has warnings");
140             else {
141                 text.append("successful");
142             }
143         }
144         return text.toString();
145     }
146 
147 }