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.batch.phases;
021    
022    import com.google.common.annotations.VisibleForTesting;
023    import org.slf4j.Logger;
024    import org.slf4j.LoggerFactory;
025    import org.sonar.api.BatchComponent;
026    import org.sonar.api.CoreProperties;
027    import org.sonar.api.config.Settings;
028    import org.sonar.api.database.DatabaseSession;
029    import org.sonar.api.database.model.Snapshot;
030    import org.sonar.api.resources.Project;
031    import org.sonar.api.resources.Scopes;
032    import org.sonar.api.utils.SonarException;
033    import org.sonar.batch.bootstrap.AnalysisMode;
034    import org.sonar.batch.bootstrap.ServerClient;
035    import org.sonar.batch.index.ResourcePersister;
036    
037    import javax.persistence.Query;
038    
039    import java.util.List;
040    
041    public class UpdateStatusJob implements BatchComponent {
042    
043      private static final Logger LOG = LoggerFactory.getLogger(UpdateStatusJob.class);
044    
045      private DatabaseSession session;
046      private ServerClient server;
047      // TODO remove this component
048      private Snapshot snapshot;
049      private ResourcePersister resourcePersister;
050      private Settings settings;
051      private Project project;
052      private AnalysisMode analysisMode;
053    
054      public UpdateStatusJob(Settings settings, ServerClient server, DatabaseSession session,
055        ResourcePersister resourcePersister, Project project, Snapshot snapshot, AnalysisMode analysisMode) {
056        this.session = session;
057        this.server = server;
058        this.resourcePersister = resourcePersister;
059        this.project = project;
060        this.snapshot = snapshot;
061        this.settings = settings;
062        this.analysisMode = analysisMode;
063      }
064    
065      public void execute() {
066        disablePreviousSnapshot();
067        enableCurrentSnapshot();
068        evictPreviewDB();
069      }
070    
071      @VisibleForTesting
072      void evictPreviewDB() {
073        if (analysisMode.isPreview()) {
074          // If this is a preview analysis then we should not evict remote preview database
075          return;
076        }
077        String url = "/batch_bootstrap/evict?project=" + project.getId();
078        try {
079          LOG.debug("Evict preview database");
080          server.request(url);
081        } catch (Exception e) {
082          throw new SonarException("Unable to evict preview database: " + url, e);
083        }
084      }
085    
086      private void disablePreviousSnapshot() {
087        // disable on all modules
088        Query query = session.createQuery("FROM " + Snapshot.class.getSimpleName() + " WHERE (root_snapshot_id=:rootId OR id=:rootId) AND scope=:scope");
089        query.setParameter("rootId", snapshot.getId());
090        query.setParameter("scope", Scopes.PROJECT);
091        List<Snapshot> moduleSnapshots = query.getResultList();
092        for (Snapshot moduleSnapshot : moduleSnapshots) {
093          Snapshot previousLastSnapshot = resourcePersister.getLastSnapshot(moduleSnapshot, true);
094          if (previousLastSnapshot != null) {
095            setFlags(previousLastSnapshot, false, null);
096          }
097        }
098      }
099    
100      private void enableCurrentSnapshot() {
101        Snapshot previousLastSnapshot = resourcePersister.getLastSnapshot(snapshot, false);
102        boolean isLast = previousLastSnapshot == null || previousLastSnapshot.getCreatedAt().before(snapshot.getCreatedAt());
103        setFlags(snapshot, isLast, Snapshot.STATUS_PROCESSED);
104        logSuccess(LoggerFactory.getLogger(getClass()));
105      }
106    
107      @VisibleForTesting
108      void logSuccess(Logger logger) {
109        if (analysisMode.isPreview()) {
110          logger.info("ANALYSIS SUCCESSFUL");
111    
112        } else {
113          String baseUrl = settings.getString(CoreProperties.SERVER_BASE_URL);
114          if (baseUrl.equals(settings.getDefaultValue(CoreProperties.SERVER_BASE_URL))) {
115            // If server base URL was not configured in Sonar server then is is better to take URL configured on batch side
116            baseUrl = server.getURL();
117          }
118          if (!baseUrl.endsWith("/")) {
119            baseUrl += "/";
120          }
121          String url = baseUrl + "dashboard/index/" + project.getKey();
122          logger.info("ANALYSIS SUCCESSFUL, you can browse {}", url);
123        }
124      }
125    
126      private void setFlags(Snapshot snapshot, boolean last, String status) {
127        String hql = "UPDATE " + Snapshot.class.getSimpleName() + " SET last=:last";
128        if (status != null) {
129          hql += ", status=:status ";
130        }
131        hql += " WHERE root_snapshot_id=:rootId OR id=:rootId OR (path LIKE :path AND root_snapshot_id=:pathRootId)";
132    
133        Query query = session.createQuery(hql);
134        if (status != null) {
135          query.setParameter("status", status);
136          snapshot.setStatus(status);
137        }
138        query.setParameter("last", last);
139        query.setParameter("rootId", snapshot.getId());
140        query.setParameter("path", snapshot.getPath() + snapshot.getId() + ".%");
141        query.setParameter("pathRootId", snapshot.getRootId() == null ? snapshot.getId() : snapshot.getRootId());
142        query.executeUpdate();
143        session.commit();
144    
145        snapshot.setLast(last);
146      }
147    }