/** @author Jason Dobies */
public class AffinityGroupSubscribedServersUIBean extends PagedDataTableUIBean {

  public static final String MANAGED_BEAN_NAME = "AffinityGroupSubscribedServersUIBean";

  private AffinityGroupManagerLocal affinityGroupManager = LookupUtil.getAffinityGroupManager();

  public String unsubscribeServers() {
    Subject subject = EnterpriseFacesContextUtility.getSubject();
    String[] selectedServerIdStrings =
        FacesContextUtility.getRequest().getParameterValues("selectedServersToUnsubscribe");

    // Parse the server IDs to ints
    Integer[] selectedServerIds = new Integer[selectedServerIdStrings.length];
    for (int ii = 0; ii < selectedServerIdStrings.length; ii++) {
      selectedServerIds[ii] = Integer.parseInt(selectedServerIdStrings[ii]);
    }

    // Update the group
    affinityGroupManager.removeServersFromGroup(subject, selectedServerIds);

    return "successOrFailure";
  }

  @Override
  public DataModel getDataModel() {
    if (dataModel == null) {
      dataModel =
          new AffinityGroupSubscribedServersDataModel(
              PageControlView.AffinityGroupSubscribedServers, MANAGED_BEAN_NAME);
    }

    return dataModel;
  }

  private class AffinityGroupSubscribedServersDataModel extends PagedListDataModel<Server> {

    private AffinityGroupSubscribedServersDataModel(PageControlView view, String beanName) {
      super(view, beanName);
    }

    @Override
    public PageList<Server> fetchPage(PageControl pc) {
      int affinityGroupId =
          FacesContextUtility.getRequiredRequestParameter("affinityGroupId", Integer.class);
      PageList<Server> results =
          affinityGroupManager.getServerMembers(getSubject(), affinityGroupId, pc);

      return results;
    }
  }
}
/** @author Jiri Kremser */
public class TopologyGWTServiceImpl extends AbstractGWTServiceImpl implements TopologyGWTService {

  private static final long serialVersionUID = 1L;

  private TopologyManagerLocal topologyManager = LookupUtil.getTopologyManager();

  private AgentManagerLocal agentManager = LookupUtil.getAgentManager();

  private PartitionEventManagerLocal partitionEventManager = LookupUtil.getPartitionEventManager();

  private AffinityGroupManagerLocal affinityGroupManager = LookupUtil.getAffinityGroupManager();

  @Override
  public PageList<ServerWithAgentCountComposite> getServers(PageControl pc)
      throws RuntimeException {
    try {
      return SerialUtility.prepare(
          topologyManager.getServerComposites(getSessionSubject(), pc),
          "TopologyGWTServiceImpl.getServers");
    } catch (Throwable t) {
      throw getExceptionToThrowToClient(t);
    }
  }

  @Override
  public void deleteServers(int[] serverIds) throws RuntimeException {
    try {
      topologyManager.deleteServers(getSessionSubject(), ArrayUtils.toObject(serverIds));
    } catch (Throwable t) {
      throw getExceptionToThrowToClient(t);
    }
  }

  @Override
  public void updateServerManualMaintenance(int[] serverIds, boolean manualMaintenance)
      throws RuntimeException {
    try {
      topologyManager.updateServerManualMaintenance(
          getSessionSubject(), ArrayUtils.toObject(serverIds), manualMaintenance);
    } catch (Throwable t) {
      throw getExceptionToThrowToClient(t);
    }
  }

  @Override
  public List<FailoverListDetails> getFailoverListDetailsByAgentId(int agentId, PageControl pc)
      throws RuntimeException {
    try {
      return SerialUtility.prepare(
          topologyManager.getFailoverListDetailsByAgentId(getSessionSubject(), agentId, pc),
          "TopologyGWTServiceImpl.getFailoverListDetailsByAgentId");
    } catch (Throwable t) {
      throw getExceptionToThrowToClient(t);
    }
  }

  @Override
  public PageList<PartitionEvent> findPartitionEventsByCriteria(PartitionEventCriteria criteria)
      throws RuntimeException {
    try {
      return SerialUtility.prepare(
          partitionEventManager.findPartitionEventsByCriteria(getSessionSubject(), criteria),
          "TopologyGWTServiceImpl.findPartitionEventsByCriteria");
    } catch (Throwable t) {
      throw getExceptionToThrowToClient(t);
    }
  }

  @Override
  public PageList<Server> findServersByCriteria(ServerCriteria criteria) throws RuntimeException {
    try {
      return SerialUtility.prepare(
          topologyManager.findServersByCriteria(getSessionSubject(), criteria),
          "TopologyGWTServiceImpl.findServersByCriteria");
    } catch (Throwable t) {
      throw getExceptionToThrowToClient(t);
    }
  }

  @Override
  public PageList<Agent> findAgentsByCriteria(AgentCriteria criteria) throws RuntimeException {
    try {
      return SerialUtility.prepare(
          agentManager.findAgentsByCriteria(getSessionSubject(), criteria),
          "TopologyGWTServiceImpl.findAgentsByCriteria");
    } catch (Throwable t) {
      throw getExceptionToThrowToClient(t);
    }
  }

  @Override
  public void cloudPartitionEventRequest() throws RuntimeException {
    try {
      partitionEventManager.cloudPartitionEventRequest(
          getSessionSubject(), PartitionEventType.ADMIN_INITIATED_PARTITION, "");
    } catch (Throwable t) {
      throw getExceptionToThrowToClient(t);
    }
  }

  @Override
  public void purgeAllEvents() throws RuntimeException {
    try {
      partitionEventManager.purgeAllEvents(getSessionSubject());
    } catch (Throwable t) {
      throw getExceptionToThrowToClient(t);
    }
  }

  @Override
  public void deletePartitionEvents(int[] eventIds) throws RuntimeException {
    try {
      partitionEventManager.deletePartitionEvents(
          getSessionSubject(), ArrayUtils.toObject(eventIds));
    } catch (Throwable t) {
      throw getExceptionToThrowToClient(t);
    }
  }

  @Override
  public PageList<PartitionEventDetails> getPartitionEventDetails(
      int partitionEventId, PageControl pageControl) throws RuntimeException {
    try {
      return SerialUtility.prepare(
          partitionEventManager.getPartitionEventDetails(
              getSessionSubject(), partitionEventId, pageControl),
          "TopologyGWTServiceImpl.getPartitionEventDetails");
    } catch (Throwable t) {
      throw getExceptionToThrowToClient(t);
    }
  }

  @Override
  public PageList<AffinityGroupCountComposite> getAffinityGroupCountComposites(
      PageControl pageControl) throws RuntimeException {
    try {
      return SerialUtility.prepare(
          affinityGroupManager.getComposites(getSessionSubject(), pageControl),
          "TopologyGWTServiceImpl.getAffinityGroupCountComposites");
    } catch (Throwable t) {
      throw getExceptionToThrowToClient(t);
    }
  }

  @Override
  public int deleteAffinityGroups(int[] affinityGroupIds) throws RuntimeException {
    try {
      return affinityGroupManager.delete(
          getSessionSubject(), ArrayUtils.toObject(affinityGroupIds));
    } catch (Throwable t) {
      throw getExceptionToThrowToClient(t);
    }
  }

  @Override
  public int createAffinityGroup(AffinityGroup affinityGroup) throws RuntimeException {
    try {
      return affinityGroupManager.create(getSessionSubject(), affinityGroup);
    } catch (Throwable t) {
      throw getExceptionToThrowToClient(t);
    }
  }

  @Override
  public AffinityGroup getAffinityGroupById(int affinityGroupId) throws RuntimeException {
    try {
      return SerialUtility.prepare(
          affinityGroupManager.getById(getSessionSubject(), affinityGroupId),
          "TopologyGWTServiceImpl.getAffinityGroupById");
    } catch (Throwable t) {
      throw getExceptionToThrowToClient(t);
    }
  }

  @Override
  public void addServersToGroup(int affinityGroupId, Integer[] serverIds) throws RuntimeException {
    try {
      affinityGroupManager.addServersToGroup(getSessionSubject(), affinityGroupId, serverIds);
    } catch (Throwable t) {
      throw getExceptionToThrowToClient(t);
    }
  }

  @Override
  public void removeServersFromGroup(Integer[] serverIds) throws RuntimeException {
    try {
      affinityGroupManager.removeServersFromGroup(getSessionSubject(), serverIds);
    } catch (Throwable t) {
      throw getExceptionToThrowToClient(t);
    }
  }

  @Override
  public void addAgentsToGroup(int affinityGroupId, Integer[] agentIds) throws RuntimeException {
    try {
      affinityGroupManager.addAgentsToGroup(getSessionSubject(), affinityGroupId, agentIds);
    } catch (Throwable t) {
      throw getExceptionToThrowToClient(t);
    }
  }

  @Override
  public void removeAgentsFromGroup(Integer[] agentIds) throws RuntimeException {
    try {
      affinityGroupManager.removeAgentsFromGroup(getSessionSubject(), agentIds);
    } catch (Throwable t) {
      throw getExceptionToThrowToClient(t);
    }
  }

  @Override
  public void updateServer(Server server) throws RuntimeException {
    try {
      topologyManager.updateServer(getSessionSubject(), server);
    } catch (Throwable t) {
      throw getExceptionToThrowToClient(t);
    }
  }

  @Override
  public void updateAffinityGroup(AffinityGroup affinityGroup) throws RuntimeException {
    try {
      affinityGroupManager.update(getSessionSubject(), affinityGroup);
    } catch (Throwable t) {
      throw getExceptionToThrowToClient(t);
    }
  }

  @Override
  public Integer getResourceIdOfAgent(int agentId) throws RuntimeException {
    try {
      return topologyManager.getResourceIdOfAgent(getSessionSubject(), agentId);
    } catch (Throwable t) {
      throw getExceptionToThrowToClient(t);
    }
  }
}