You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultModelManager.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Sonar, open source software quality management tool.
  3. * Copyright (C) 2009 SonarSource SA
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * Sonar is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * Sonar is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Sonar; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.server.qualitymodel;
  21. import org.apache.commons.lang.StringUtils;
  22. import org.slf4j.LoggerFactory;
  23. import org.sonar.api.ServerComponent;
  24. import org.sonar.api.database.DatabaseSession;
  25. import org.sonar.api.qualitymodel.Model;
  26. import org.sonar.api.qualitymodel.ModelDefinition;
  27. import org.sonar.api.utils.Logs;
  28. import org.sonar.api.utils.SonarException;
  29. import org.sonar.jpa.session.DatabaseSessionFactory;
  30. import javax.persistence.Query;
  31. public final class DefaultModelManager implements ServerComponent, ModelManager {
  32. private ModelDefinition[] definitions;
  33. private DatabaseSessionFactory sessionFactory;
  34. public DefaultModelManager(DatabaseSessionFactory sessionFactory, ModelDefinition[] definitions) {
  35. this.sessionFactory = sessionFactory;
  36. this.definitions = definitions;
  37. }
  38. /**
  39. * This constructor is used when there are no templates
  40. */
  41. public DefaultModelManager(DatabaseSessionFactory sessionFactory) {
  42. this.sessionFactory = sessionFactory;
  43. this.definitions = new ModelDefinition[0];
  44. }
  45. /**
  46. * Executed when the server starts
  47. */
  48. public ModelManager registerDefinitions() {
  49. DatabaseSession session = sessionFactory.getSession();
  50. for (ModelDefinition definition : definitions) {
  51. if (StringUtils.isNotBlank(definition.getName()) && !exists(session, definition.getName())) {
  52. Logs.INFO.info("Register quality model: " + definition.getName());
  53. Model model = definition.create();
  54. if (StringUtils.isBlank(model.getName())) {
  55. model.setName(definition.getName());
  56. }
  57. insert(session, model);
  58. session.commit();
  59. }
  60. }
  61. return this;
  62. }
  63. public Model reset(String name) {
  64. ModelDefinition definition = findDefinitionByName(name);
  65. if (definition == null) {
  66. throw new SonarException("Can not reset quality model. Definition not found: " + name);
  67. }
  68. LoggerFactory.getLogger(getClass()).info("Reset quality model: " + name);
  69. Model model = definition.create();
  70. return reset(model);
  71. }
  72. Model reset(Model model) {
  73. DatabaseSession session = sessionFactory.getSession();
  74. try {
  75. delete(session, model.getName());
  76. model = insert(session, model);
  77. session.commit();
  78. return model;
  79. } catch (RuntimeException e) {
  80. session.rollback();
  81. throw e;
  82. }
  83. }
  84. public ModelDefinition findDefinitionByName(String name) {
  85. for (ModelDefinition definition : definitions) {
  86. if (StringUtils.equals(name, definition.getName())) {
  87. return definition;
  88. }
  89. }
  90. return null;
  91. }
  92. public static void delete(DatabaseSession session, String name) {
  93. Model model = session.getSingleResult(Model.class, "name", name);
  94. if (model != null) {
  95. session.removeWithoutFlush(model);
  96. session.commit();
  97. }
  98. }
  99. public static Model insert(DatabaseSession session, Model model) {
  100. return (Model) session.saveWithoutFlush(model);
  101. }
  102. public static boolean exists(DatabaseSession session, String name) {
  103. Query query = session.getEntityManager().createQuery("SELECT COUNT(qm) FROM " + Model.class.getSimpleName() + " qm WHERE qm.name=:name");
  104. query.setParameter("name", name);
  105. Number count = (Number) query.getSingleResult();
  106. return count.intValue() > 0;
  107. }
  108. }