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 */
020 package org.sonar.server.qualitymodel;
021
022 import org.apache.commons.lang.StringUtils;
023 import org.slf4j.LoggerFactory;
024 import org.sonar.api.ServerComponent;
025 import org.sonar.api.database.DatabaseSession;
026 import org.sonar.api.qualitymodel.Model;
027 import org.sonar.api.qualitymodel.ModelDefinition;
028 import org.sonar.api.utils.Logs;
029 import org.sonar.api.utils.SonarException;
030 import org.sonar.jpa.session.DatabaseSessionFactory;
031
032 import javax.persistence.Query;
033
034 public final class DefaultModelManager implements ServerComponent, ModelManager {
035
036 private ModelDefinition[] definitions;
037 private DatabaseSessionFactory sessionFactory;
038
039 public DefaultModelManager(DatabaseSessionFactory sessionFactory, ModelDefinition[] definitions) {
040 this.sessionFactory = sessionFactory;
041 this.definitions = definitions;
042 }
043
044 /**
045 * This constructor is used when there are no templates
046 */
047 public DefaultModelManager(DatabaseSessionFactory sessionFactory) {
048 this.sessionFactory = sessionFactory;
049 this.definitions = new ModelDefinition[0];
050 }
051
052 /**
053 * Executed when the server starts
054 */
055 public ModelManager registerDefinitions() {
056 DatabaseSession session = sessionFactory.getSession();
057 for (ModelDefinition definition : definitions) {
058 if (StringUtils.isNotBlank(definition.getName()) && !exists(session, definition.getName())) {
059 Logs.INFO.info("Register quality model: " + definition.getName());
060 Model model = definition.createModel();
061 if (StringUtils.isBlank(model.getName())) {
062 model.setName(definition.getName());
063 }
064 insert(session, model);
065 session.commit();
066 }
067 }
068 return this;
069 }
070
071 public Model reset(String name) {
072 ModelDefinition definition = findDefinitionByName(name);
073 if (definition == null) {
074 throw new SonarException("Can not reset quality model. Definition not found: " + name);
075 }
076
077 LoggerFactory.getLogger(getClass()).info("Reset quality model: " + name);
078 Model model = definition.createModel();
079 return reset(model);
080 }
081
082
083 Model reset(Model model) {
084 DatabaseSession session = sessionFactory.getSession();
085 try {
086 delete(session, model.getName());
087 model = insert(session, model);
088 session.commit();
089 return model;
090
091 } catch (RuntimeException e) {
092 session.rollback();
093 throw e;
094 }
095 }
096
097 public ModelDefinition findDefinitionByName(String name) {
098 for (ModelDefinition definition : definitions) {
099 if (StringUtils.equals(name, definition.getName())) {
100 return definition;
101 }
102 }
103 return null;
104 }
105
106 public static void delete(DatabaseSession session, String name) {
107 Model model = session.getSingleResult(Model.class, "name", name);
108 if (model != null) {
109 session.removeWithoutFlush(model);
110 session.commit();
111 }
112 }
113
114 public static Model insert(DatabaseSession session, Model model) {
115 return (Model) session.saveWithoutFlush(model);
116 }
117
118 public static boolean exists(DatabaseSession session, String name) {
119 Query query = session.getEntityManager().createQuery("SELECT COUNT(qm) FROM " + Model.class.getSimpleName() + " qm WHERE qm.name=:name");
120 query.setParameter("name", name);
121 Number count = (Number) query.getSingleResult();
122 return count.intValue() > 0;
123 }
124 }