001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
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 */
020package org.sonar.batch.phases;
021
022import org.slf4j.LoggerFactory;
023import org.sonar.api.BatchComponent;
024import org.sonar.api.database.DatabaseSession;
025import org.sonar.api.database.model.Snapshot;
026import org.sonar.api.resources.Scopes;
027import org.sonar.batch.ServerMetadata;
028import org.sonar.batch.index.ResourcePersister;
029import org.sonar.core.NotDryRun;
030
031import javax.persistence.Query;
032import java.util.List;
033
034@NotDryRun
035public class UpdateStatusJob implements BatchComponent {
036
037  private DatabaseSession session;
038  private ServerMetadata server;
039  private Snapshot snapshot; // TODO remove this component
040  private ResourcePersister resourcePersister;
041
042  public UpdateStatusJob(ServerMetadata server, DatabaseSession session, ResourcePersister resourcePersister, Snapshot snapshot) {
043    this.session = session;
044    this.server = server;
045    this.resourcePersister = resourcePersister;
046    this.snapshot = snapshot;
047  }
048
049  public void execute() {
050    disablePreviousSnapshot();
051    enableCurrentSnapshot();
052  }
053
054  private void disablePreviousSnapshot() {
055    // disable on all modules
056    Query query = session.createQuery("FROM " + Snapshot.class.getSimpleName() + " WHERE (root_snapshot_id=:rootId OR id=:rootId) AND scope=:scope");
057    query.setParameter("rootId", snapshot.getId());
058    query.setParameter("scope", Scopes.PROJECT);
059    List<Snapshot> moduleSnapshots = query.getResultList();
060    for (Snapshot moduleSnapshot : moduleSnapshots) {
061      Snapshot previousLastSnapshot = resourcePersister.getLastSnapshot(moduleSnapshot, true);
062      if (previousLastSnapshot != null) {
063        setFlags(previousLastSnapshot, false, null);
064      }
065    }
066  }
067
068  private void enableCurrentSnapshot() {
069    Snapshot previousLastSnapshot = resourcePersister.getLastSnapshot(snapshot, false);
070    boolean isLast = (previousLastSnapshot == null || previousLastSnapshot.getCreatedAt().before(snapshot.getCreatedAt()));
071    setFlags(snapshot, isLast, Snapshot.STATUS_PROCESSED);
072    LoggerFactory.getLogger(getClass()).info("ANALYSIS SUCCESSFUL, you can browse {}", server.getURL());
073  }
074
075  private void setFlags(Snapshot snapshot, boolean last, String status) {
076    String hql = "UPDATE " + Snapshot.class.getSimpleName() + " SET last=:last";
077    if (status != null) {
078      hql += ", status=:status ";
079    }
080    hql += " WHERE root_snapshot_id=:rootId OR id=:rootId OR (path LIKE :path AND root_snapshot_id=:pathRootId)";
081
082    Query query = session.createQuery(hql);
083    if (status != null) {
084      query.setParameter("status", status);
085      snapshot.setStatus(status);
086    }
087    query.setParameter("last", last);
088    query.setParameter("rootId", snapshot.getId());
089    query.setParameter("path", snapshot.getPath() + snapshot.getId() + ".%");
090    query.setParameter("pathRootId", (snapshot.getRootId() == null ? snapshot.getId() : snapshot.getRootId()));
091    query.executeUpdate();
092    session.commit();
093
094    snapshot.setLast(last);
095  }
096}