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 */
020package org.sonar.wsclient;
021
022import org.sonar.wsclient.connectors.Connector;
023import org.sonar.wsclient.connectors.ConnectorFactory;
024import org.sonar.wsclient.services.*;
025import org.sonar.wsclient.unmarshallers.UnmarshalException;
026import org.sonar.wsclient.unmarshallers.Unmarshaller;
027import org.sonar.wsclient.unmarshallers.Unmarshallers;
028
029import java.util.Collections;
030import java.util.List;
031
032public class Sonar {
033
034  static {
035    WSUtils.setInstance(new JdkUtils());
036  }
037
038  private Connector connector;
039
040  public Sonar(Connector connector) {
041    this.connector = connector;
042  }
043
044  public Connector getConnector() {
045    return connector;
046  }
047
048  public <M extends Model> M find(Query<M> query) {
049    String json = connector.execute(query);
050    M result = null;
051    if (json != null) {
052      try {
053        Unmarshaller<M> unmarshaller = Unmarshallers.forModel(query.getModelClass());
054        result = unmarshaller.toModel(json);
055      } catch (Exception e) {
056        throw new UnmarshalException(query, json, e);
057      }
058    }
059    return result;
060  }
061
062  public <M extends Model> List<M> findAll(Query<M> query) {
063    String json = connector.execute(query);
064    List<M> result;
065    if (json == null) {
066      result = Collections.emptyList();
067    } else {
068      try {
069        Unmarshaller<M> unmarshaller = Unmarshallers.forModel(query.getModelClass());
070        result = unmarshaller.toModels(json);
071      } catch (Exception e) {
072        throw new UnmarshalException(query, json, e);
073      }
074    }
075    return result;
076  }
077
078  public <M extends Model> M create(CreateQuery<M> query) {
079    String json = connector.execute(query);
080    M result = null;
081    if (json != null) {
082      try {
083        Unmarshaller<M> unmarshaller = Unmarshallers.forModel(query.getModelClass());
084        result = unmarshaller.toModel(json);
085      } catch (Exception e) {
086        throw new UnmarshalException(query, json, e);
087      }
088    }
089    return result;
090  }
091
092  public void update(UpdateQuery<?> query) {
093    connector.execute(query);
094  }
095
096  public void delete(DeleteQuery query) {
097    connector.execute(query);
098  }
099
100  public static Sonar create(String host) {
101    return new Sonar(ConnectorFactory.create(new Host(host)));
102  }
103
104  public static Sonar create(String host, String username, String password) {
105    return new Sonar(ConnectorFactory.create(new Host(host, username, password)));
106  }
107}