/*
  * (non-Javadoc)
  *
  * @see
  * org.jboss.forge.addon.deltaspike.commands.AbstractDeltaSpikeCommand#prepareModulesList(org.jboss.forge.addon.deltaspike
  * .facets.DeltaSpikeFacet, org.jboss.forge.addon.ui.input.UISelectMany)
  */
 @Override
 public void prepareModulesList(
     DeltaSpikeFacet deltaSpikeFacet, UISelectMany<DeltaSpikeModule> dsModules) {
   dsModules.setRequired(false);
   dsModules.setValueChoices(Arrays.<DeltaSpikeModule>asList(DeltaSpikeModules.values()));
   dsModules.setValue(deltaSpikeFacet.getInstalledModules());
 }
 /*
  * (non-Javadoc)
  *
  * @see org.jboss.forge.addon.deltaspike.facets.DeltaSpikeFacet#getInstalledModules()
  */
 @Override
 public Set<DeltaSpikeModule> getInstalledModules() {
   Set<DeltaSpikeModule> installedModules = new HashSet<DeltaSpikeModule>();
   for (DeltaSpikeModules module : DeltaSpikeModules.values()) {
     if (isModuleInstalled(module)) {
       installedModules.add(module);
     }
   }
   return installedModules;
 }
  @Override
  public Result execute(UIExecutionContext context) throws Exception {
    Project project = getSelectedProject(context);
    DeltaSpikeFacet deltaSpikeFacet = project.getFacet(DeltaSpikeFacet.class);

    Iterable<DeltaSpikeModule> selectedModules = dsModules.getValue();
    Set<DeltaSpikeModule> modulesInstalled = new HashSet<DeltaSpikeModule>();
    Set<DeltaSpikeModule> modulesRemoved = new HashSet<DeltaSpikeModule>();
    for (DeltaSpikeModule dsModule : DeltaSpikeModules.values()) {
      boolean selected = false;
      for (DeltaSpikeModule selectedModule : selectedModules) {
        if (selectedModule.equals(dsModule)) {
          selected = true;
        }
      }
      // Modules to Install
      if (selected && !deltaSpikeFacet.isModuleInstalled(dsModule)) {
        modulesInstalled.add(dsModule);
        deltaSpikeFacet.install(dsModule);
        if (dsModule.getInstallationExtraStep() != null) {
          dsModule.getInstallationExtraStep().install(project);
        }
      }
      // Modules to Remove
      if (!selected && deltaSpikeFacet.isModuleInstalled(dsModule)) {
        modulesRemoved.add(dsModule);
        deltaSpikeFacet.remove(dsModule);
        if (dsModule.getInstallationExtraStep() != null) {
          dsModule.getInstallationExtraStep().remove(project);
        }
      }
    }

    return Results.success(
        "DeltaSpike modules installed:"
            + modulesInstalled
            + "\nDeltaSpike modules removed:"
            + modulesRemoved);
  }