001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar 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     * Sonar 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
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.wsclient.services;
021    
022    /**
023     * The web service "dependency" is since Sonar 2.0
024     */
025    public class DependencyQuery extends Query<Dependency> {
026      public static final String BASE_URL = "/api/dependencies";
027    
028      private String resourceIdOrKey = null;
029      private String direction = null;
030      private String parentId = null;
031      private String id = null;
032      public static final String INCOMING_DIRECTION = "in";
033      public static final String OUTGOING_DIRECTION = "out";
034    
035      public String getResourceIdOrKey() {
036        return resourceIdOrKey;
037      }
038    
039      public DependencyQuery setResourceIdOrKey(String resourceIdOrKey) {
040        this.resourceIdOrKey = resourceIdOrKey;
041        return this;
042      }
043    
044      public DependencyQuery setResourceId(long resourceId) {
045        this.resourceIdOrKey = String.valueOf(resourceId);
046        return this;
047      }
048    
049      public String getDirection() {
050        return direction;
051      }
052    
053      public DependencyQuery setDirection(String direction) {
054        this.direction = direction;
055        return this;
056      }
057    
058      @Override
059      public String getUrl() {
060        StringBuilder url = new StringBuilder(BASE_URL);
061        url.append('?');
062        if (resourceIdOrKey != null) {
063          url.append("resource=").append(resourceIdOrKey).append('&');
064        }
065        if (direction != null) {
066          url.append("dir=").append(direction).append('&');
067        }
068        if (parentId !=null) {
069          url.append("parent=").append(parentId).append('&');
070        }
071        if (id !=null) {
072          url.append("id=").append(id).append('&');
073        }
074        return url.toString();
075      }
076    
077      public String getParentId() {
078        return parentId;
079      }
080    
081      public String getId() {
082        return id;
083      }
084    
085      public DependencyQuery setId(String id) {
086        this.id = id;
087        return this;
088      }
089    
090      public DependencyQuery setParentId(String parentId) {
091        this.parentId = parentId;
092        return this;
093      }
094    
095      @Override
096      public Class<Dependency> getModelClass() {
097        return Dependency.class;
098      }
099    
100      /**
101       * Resources that depend upon a resource
102       *
103       * @param resourceIdOrKey the target resource. Can be the primary key (a number) or the logical key (String)
104       */
105      public static DependencyQuery createForIncomingDependencies(String resourceIdOrKey) {
106        DependencyQuery query = new DependencyQuery();
107        query.setResourceIdOrKey(resourceIdOrKey);
108        query.setDirection(INCOMING_DIRECTION);
109        return query;
110      }
111    
112      /**
113       * Resources that are depended upon a resource = all the resources that a resource depends upon
114       *
115       * @param resourceIdOrKey the target resource. Can be the primary key (an integer) or the logical key (String)
116       */
117      public static DependencyQuery createForOutgoingDependencies(String resourceIdOrKey) {
118        DependencyQuery query = new DependencyQuery();
119        query.setResourceIdOrKey(resourceIdOrKey);
120        query.setDirection(OUTGOING_DIRECTION);
121        return query;
122      }
123    
124      /**
125       * Resources that depend upon or are depended upon a resource. It equals the merge of createForIncomingDependencies(resourceIdOrKey)
126       * and createForOutgoingDependencies(resourceIdOrKey)
127       *
128       * @param resourceIdOrKey the target resource. Can be the primary key (an integer) or the logical key (String)
129       */
130      public static DependencyQuery createForResource(String resourceIdOrKey) {
131        DependencyQuery query = new DependencyQuery();
132        query.setResourceIdOrKey(resourceIdOrKey);
133        return query;
134      }
135    
136      public static DependencyQuery createForResource(long resourceId) {
137        DependencyQuery query = new DependencyQuery();
138        query.setResourceId(resourceId);
139        return query;
140      }
141    
142      public static DependencyQuery createForSubDependencies(String dependencyId) {
143        DependencyQuery query = new DependencyQuery();
144        query.setParentId(dependencyId);
145        return query;
146      }
147    
148      public static DependencyQuery createForId(String id) {
149        DependencyQuery query = new DependencyQuery();
150        query.setId(id);
151        return query;
152      }
153    }