Coverage Report - org.jtheque.core.managers.module.ModuleConfiguration
 
Classes in this File Line Coverage Branch Coverage Complexity
ModuleConfiguration
0 %
0/58
0 %
0/30
2.2
 
 1  
 package org.jtheque.core.managers.module;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jtheque.core.managers.module.beans.ModuleContainer;
 20  
 import org.jtheque.core.managers.module.beans.ModuleState;
 21  
 import org.jtheque.core.managers.state.AbstractState;
 22  
 import org.jtheque.core.managers.state.NodeState;
 23  
 import org.jtheque.core.managers.update.InstallationResult;
 24  
 import org.jtheque.utils.StringUtils;
 25  
 
 26  
 import java.util.ArrayList;
 27  
 import java.util.Collection;
 28  
 
 29  
 /**
 30  
  * A module configuration.
 31  
  *
 32  
  * @author Baptiste Wicht
 33  
  */
 34  0
 public final class ModuleConfiguration extends AbstractState {
 35  0
     private final Collection<ModuleInfo> infos = new ArrayList<ModuleInfo>(20);
 36  
 
 37  
     private boolean discovery;
 38  
 
 39  
     @Override
 40  
     public boolean isDelegated() {
 41  0
         return true;
 42  
     }
 43  
 
 44  
     @Override
 45  
     public void delegateLoad(Collection<NodeState> nodes) {
 46  0
         for (NodeState node : nodes){
 47  0
             if ("module".equals(node.getName())) {
 48  0
                 infos.add(convertToModuleInfo(node));
 49  0
             } else if("discovery".equals(node.getName())){
 50  0
                 discovery = true;
 51  
             }
 52  
         }
 53  0
     }
 54  
 
 55  
     /**
 56  
      * Convert the node state to a ModuleInfo.
 57  
      *
 58  
      * @param node The node state to convert to ModuleInfo.
 59  
      *
 60  
      * @return The ModuleInfo.
 61  
      */
 62  
     private static ModuleInfo convertToModuleInfo(NodeState node) {
 63  0
         ModuleInfo info = new ModuleInfo(node.getAttributeValue("id"));
 64  
 
 65  0
         if(StringUtils.isNotEmpty(node.getAttributeValue("state"))){
 66  0
             info.setState(ModuleState.valueOf(node.getIntAttributeValue("state")));
 67  
         } else {
 68  0
             for (NodeState child : node.getChildrens()) {
 69  0
                 if ("state".equals(child.getName())) {
 70  0
                     info.setState(ModuleState.valueOf(Integer.parseInt(child.getText())));
 71  
                 }
 72  
             }
 73  
         }
 74  
 
 75  0
         if(info.getState() == null){
 76  0
             info.setState(ModuleState.INSTALLED);
 77  
         }
 78  
 
 79  0
         return info;
 80  
     }
 81  
 
 82  
     @Override
 83  
     public Collection<NodeState> delegateSave() {
 84  0
         Collection<NodeState> states = new ArrayList<NodeState>(25);
 85  
 
 86  0
         if(discovery){
 87  0
             states.add(new NodeState("discovery"));
 88  
         }
 89  
 
 90  0
         for (ModuleInfo info : infos) {
 91  0
             states.add(convertToNodeState(info));
 92  
         }
 93  
 
 94  0
         return states;
 95  
     }
 96  
 
 97  
     /**
 98  
      * Convert the module info the node state.
 99  
      *
 100  
      * @param info The module info.
 101  
      *
 102  
      * @return The node state.
 103  
      */
 104  
     private static NodeState convertToNodeState(ModuleInfo info) {
 105  0
         NodeState state = new NodeState("module");
 106  
 
 107  0
         state.setAttribute("id", info.getModuleId());
 108  0
         state.setAttribute("state", Integer.toString(info.getState().ordinal()));
 109  
 
 110  0
         return state;
 111  
     }
 112  
 
 113  
     /**
 114  
      * Return the module information of a module.
 115  
      *
 116  
      * @param moduleName The name of the module.
 117  
      *
 118  
      * @return The module information.
 119  
      */
 120  
     ModuleInfo getModuleInfo(String moduleName) {
 121  0
         for (ModuleInfo i : infos) {
 122  0
             if (i.getModuleId().equals(moduleName)) {
 123  0
                 return i;
 124  
             }
 125  
         }
 126  
 
 127  0
         return null;
 128  
     }
 129  
 
 130  
     /**
 131  
      * Indicate if we must add the modules not listed in the XML File or not.
 132  
      *
 133  
      * @return <code>true</code> if we must add the modules not listed in the configuration else <code>false</code>. 
 134  
      */
 135  
     public boolean isDiscovery(){
 136  0
         return discovery;
 137  
     }
 138  
 
 139  
     /**
 140  
      * Return the state of the module.
 141  
      *
 142  
      * @param moduleName The name of the module.
 143  
      * @return The state of the module.
 144  
      */
 145  
     public ModuleState getState(String moduleName) {
 146  0
         ModuleInfo info = getModuleInfo(moduleName);
 147  
 
 148  0
         if (info != null) {
 149  0
             return info.getState();
 150  
         }
 151  
 
 152  0
         return null;
 153  
     }
 154  
 
 155  
     /**
 156  
      * Set the state of the module.
 157  
      *
 158  
      * @param moduleName The name of the module.
 159  
      * @param state      The state.
 160  
      */
 161  
     public void setState(String moduleName, ModuleState state) {
 162  0
         ModuleInfo info = getModuleInfo(moduleName);
 163  
 
 164  0
         if (info != null) {
 165  0
             info.setState(state);
 166  
         }
 167  0
     }
 168  
 
 169  
     /**
 170  
      * Indicate if the configuration contains a module or not.
 171  
      *
 172  
      * @param module The module to test.
 173  
      * @return true if the manager contains the module.
 174  
      */
 175  
     public boolean containsModule(ModuleContainer module) {
 176  0
         for (ModuleInfo i : infos) {
 177  0
             if (i.getModuleId().equals(module.getId())) {
 178  0
                 return true;
 179  
             }
 180  
         }
 181  
 
 182  0
         return false;
 183  
     }
 184  
 
 185  
     /**
 186  
      * Remove the module.
 187  
      *
 188  
      * @param module The module to remove.
 189  
      */
 190  
     public void remove(ModuleContainer module) {
 191  0
         ModuleInfo info = getModuleInfo(module.getId());
 192  
 
 193  0
         infos.remove(info);
 194  0
     }
 195  
 
 196  
     /**
 197  
      * Add a module container to the configuration.
 198  
      *
 199  
      * @param module The module container to add.
 200  
      */
 201  
     public void add(ModuleContainer module) {
 202  0
         add(module.getId(), module.getState());
 203  0
     }
 204  
 
 205  
     /**
 206  
      * Add a module to the configuration.
 207  
      *
 208  
      * @param module The module to add.
 209  
      * @param state  The state of the module.
 210  
      */
 211  
     public void add(ModuleContainer module, ModuleState state) {
 212  0
         add(module.getId(), state);
 213  0
     }
 214  
 
 215  
     /**
 216  
      * Add the result of an installation to the module configuration.
 217  
      *
 218  
      * @param result The installation result.
 219  
      */
 220  
     public void add(InstallationResult result) {
 221  0
         add(result.getName(), ModuleState.INSTALLED);
 222  0
     }
 223  
 
 224  
     /**
 225  
      * Add a module info to the configuration.
 226  
      *
 227  
      * @param id The module id.
 228  
      * @param state The module state.
 229  
      */
 230  
     private void add(String id, ModuleState state) {
 231  0
         ModuleInfo info = new ModuleInfo(id);
 232  
 
 233  0
         info.setState(state);
 234  
 
 235  0
         infos.add(info);
 236  0
     }
 237  
 }