001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * SonarQube is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.wsclient.issue.internal;
021    
022    import org.json.simple.JSONValue;
023    import org.sonar.wsclient.internal.EncodingUtils;
024    import org.sonar.wsclient.internal.HttpRequestFactory;
025    import org.sonar.wsclient.issue.*;
026    
027    import java.util.ArrayList;
028    import java.util.List;
029    import java.util.Map;
030    
031    /**
032     * Do not instantiate this class, but use {@link org.sonar.wsclient.SonarClient#actionPlanClient()}.
033     */
034    public class DefaultActionPlanClient implements ActionPlanClient {
035    
036      private final HttpRequestFactory requestFactory;
037    
038      /**
039       * For internal use. Use {@link org.sonar.wsclient.SonarClient} to get an instance.
040       */
041      public DefaultActionPlanClient(HttpRequestFactory requestFactory) {
042        this.requestFactory = requestFactory;
043      }
044    
045      @Override
046      public List<ActionPlan> find(String projectKey) {
047        String json = requestFactory.get(ActionPlanQuery.BASE_URL, EncodingUtils.toMap("project", projectKey));
048        List<ActionPlan> result = new ArrayList<ActionPlan>();
049        Map jsonRoot = (Map) JSONValue.parse(json);
050        List<Map> jsonActionPlans = (List<Map>) jsonRoot.get("actionPlans");
051        if (jsonActionPlans != null) {
052          for (Map jsonActionPlan : jsonActionPlans) {
053            result.add(new DefaultActionPlan(jsonActionPlan));
054          }
055        }
056        return result;
057      }
058    
059      @Override
060      public ActionPlan create(NewActionPlan newActionPlan) {
061        String json = requestFactory.post("/api/action_plans/create", newActionPlan.urlParams());
062        return createActionPlanResult(json);
063      }
064    
065      @Override
066      public ActionPlan update(UpdateActionPlan updateActionPlan) {
067        String json = requestFactory.post("/api/action_plans/update", updateActionPlan.urlParams());
068        return createActionPlanResult(json);
069      }
070    
071      @Override
072      public void delete(String actionPlanKey) {
073        executeSimpleAction(actionPlanKey, "delete");
074      }
075    
076      @Override
077      public ActionPlan open(String actionPlanKey) {
078        String json = executeSimpleAction(actionPlanKey, "open");
079        return createActionPlanResult(json);
080      }
081    
082      @Override
083      public ActionPlan close(String actionPlanKey) {
084        String json = executeSimpleAction(actionPlanKey, "close");
085        return createActionPlanResult(json);
086      }
087    
088      private String executeSimpleAction(String actionPlanKey, String action) {
089        return requestFactory.post("/api/action_plans/" + action, EncodingUtils.toMap("key", actionPlanKey));
090      }
091    
092      private ActionPlan createActionPlanResult(String json) {
093        Map jsonRoot = (Map) JSONValue.parse(json);
094        return new DefaultActionPlan((Map) jsonRoot.get("actionPlan"));
095      }
096    
097    }