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.

ResourceDao.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube 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. * SonarQube 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 License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.core.resource;
  21. import com.google.common.base.Function;
  22. import com.google.common.collect.Iterables;
  23. import org.apache.ibatis.session.SqlSession;
  24. import org.sonar.api.component.Component;
  25. import org.sonar.api.resources.Scopes;
  26. import org.sonar.api.utils.System2;
  27. import org.sonar.api.utils.internal.Uuids;
  28. import org.sonar.core.component.ComponentDto;
  29. import org.sonar.core.component.SnapshotDto;
  30. import org.sonar.core.persistence.DaoComponent;
  31. import org.sonar.core.persistence.DbSession;
  32. import org.sonar.core.persistence.MyBatis;
  33. import javax.annotation.CheckForNull;
  34. import javax.annotation.Nullable;
  35. import java.util.Collection;
  36. import java.util.Collections;
  37. import java.util.Date;
  38. import java.util.List;
  39. import static com.google.common.collect.Lists.newArrayList;
  40. public class ResourceDao implements DaoComponent {
  41. private MyBatis mybatis;
  42. private System2 system2;
  43. public ResourceDao(MyBatis mybatis, System2 system2) {
  44. this.mybatis = mybatis;
  45. this.system2 = system2;
  46. }
  47. public List<ResourceDto> getResources(ResourceQuery query) {
  48. SqlSession session = mybatis.openSession(false);
  49. try {
  50. return session.getMapper(ResourceMapper.class).selectResources(query);
  51. } finally {
  52. MyBatis.closeQuietly(session);
  53. }
  54. }
  55. public List<ResourceDto> getResources(ResourceQuery query, SqlSession session) {
  56. return session.getMapper(ResourceMapper.class).selectResources(query);
  57. }
  58. /**
  59. * Return a single result or null. If the request returns multiple rows, then
  60. * the first row is returned.
  61. */
  62. @CheckForNull
  63. public ResourceDto getResource(ResourceQuery query) {
  64. DbSession session = mybatis.openSession(false);
  65. try {
  66. return getResource(query, session);
  67. } finally {
  68. MyBatis.closeQuietly(session);
  69. }
  70. }
  71. @CheckForNull
  72. public ResourceDto getResource(ResourceQuery query, DbSession session) {
  73. List<ResourceDto> resources = getResources(query, session);
  74. if (!resources.isEmpty()) {
  75. return resources.get(0);
  76. }
  77. return null;
  78. }
  79. public List<Long> getResourceIds(ResourceQuery query) {
  80. SqlSession session = mybatis.openSession(false);
  81. try {
  82. return session.getMapper(ResourceMapper.class).selectResourceIds(query);
  83. } finally {
  84. MyBatis.closeQuietly(session);
  85. }
  86. }
  87. public ResourceDto getResource(long projectId) {
  88. SqlSession session = mybatis.openSession(false);
  89. try {
  90. return getResource(projectId, session);
  91. } finally {
  92. MyBatis.closeQuietly(session);
  93. }
  94. }
  95. @CheckForNull
  96. public ResourceDto getResource(String componentUuid) {
  97. SqlSession session = mybatis.openSession(false);
  98. try {
  99. return session.getMapper(ResourceMapper.class).selectResourceByUuid(componentUuid);
  100. } finally {
  101. MyBatis.closeQuietly(session);
  102. }
  103. }
  104. public ResourceDto getResource(long projectId, SqlSession session) {
  105. return session.getMapper(ResourceMapper.class).selectResource(projectId);
  106. }
  107. @CheckForNull
  108. public SnapshotDto getLastSnapshot(String resourceKey, SqlSession session) {
  109. return session.getMapper(ResourceMapper.class).selectLastSnapshotByResourceKey(resourceKey);
  110. }
  111. @CheckForNull
  112. public SnapshotDto getLastSnapshotByResourceUuid(String componentUuid, SqlSession session) {
  113. return session.getMapper(ResourceMapper.class).selectLastSnapshotByResourceUuid(componentUuid);
  114. }
  115. public List<ResourceDto> getDescendantProjects(long projectId) {
  116. SqlSession session = mybatis.openSession(false);
  117. try {
  118. return getDescendantProjects(projectId, session);
  119. } finally {
  120. MyBatis.closeQuietly(session);
  121. }
  122. }
  123. public List<ResourceDto> getDescendantProjects(long projectId, SqlSession session) {
  124. ResourceMapper mapper = session.getMapper(ResourceMapper.class);
  125. List<ResourceDto> resources = newArrayList();
  126. appendChildProjects(projectId, mapper, resources);
  127. return resources;
  128. }
  129. private void appendChildProjects(long projectId, ResourceMapper mapper, List<ResourceDto> resources) {
  130. List<ResourceDto> subProjects = mapper.selectDescendantProjects(projectId);
  131. for (ResourceDto subProject : subProjects) {
  132. resources.add(subProject);
  133. appendChildProjects(subProject.getId(), mapper, resources);
  134. }
  135. }
  136. /**
  137. * Used by the Views Plugin
  138. */
  139. public ResourceDao insertOrUpdate(ResourceDto... resources) {
  140. SqlSession session = mybatis.openSession(false);
  141. ResourceMapper mapper = session.getMapper(ResourceMapper.class);
  142. Date now = new Date(system2.now());
  143. try {
  144. for (ResourceDto resource : resources) {
  145. if (resource.getId() == null) {
  146. // Fix for Views
  147. if (resource.getUuid() == null && Scopes.PROJECT.equals(resource.getScope())) {
  148. String uuid = Uuids.create();
  149. resource.setUuid(uuid);
  150. resource.setProjectUuid(uuid);
  151. resource.setModuleUuidPath("");
  152. }
  153. resource.setCreatedAt(now);
  154. resource.setAuthorizationUpdatedAt(now.getTime());
  155. mapper.insert(resource);
  156. } else {
  157. mapper.update(resource);
  158. }
  159. }
  160. session.commit();
  161. } finally {
  162. MyBatis.closeQuietly(session);
  163. }
  164. return this;
  165. }
  166. /**
  167. * Should not be called from batch side (used to reindex permission in E/S)
  168. */
  169. public void updateAuthorizationDate(Long projectId, SqlSession session) {
  170. session.getMapper(ResourceMapper.class).updateAuthorizationDate(projectId, system2.now());
  171. }
  172. @CheckForNull
  173. public Component findByKey(String key) {
  174. ResourceDto resourceDto = getResource(ResourceQuery.create().setKey(key));
  175. return resourceDto != null ? toComponent(resourceDto) : null;
  176. }
  177. @CheckForNull
  178. public Component findById(Long id, SqlSession session) {
  179. ResourceDto resourceDto = getResource(id, session);
  180. return resourceDto != null ? toComponent(resourceDto) : null;
  181. }
  182. /**
  183. * Return the root project of a component.
  184. * Will return the component itself if it's already the root project
  185. * Can return null if the component does not exists.
  186. *
  187. * The implementation should rather use a new column already containing the root project, see https://jira.sonarsource.com/browse/SONAR-5188.
  188. */
  189. @CheckForNull
  190. public ResourceDto getRootProjectByComponentKey(DbSession session, String componentKey) {
  191. ResourceDto component = getResource(ResourceQuery.create().setKey(componentKey), session);
  192. if (component != null) {
  193. Long rootId = component.getRootId();
  194. if (rootId != null) {
  195. return getParentModuleByComponentId(rootId, session);
  196. } else {
  197. return component;
  198. }
  199. }
  200. return null;
  201. }
  202. @CheckForNull
  203. public ResourceDto getRootProjectByComponentKey(String componentKey) {
  204. DbSession session = mybatis.openSession(false);
  205. try {
  206. return getRootProjectByComponentKey(session, componentKey);
  207. } finally {
  208. MyBatis.closeQuietly(session);
  209. }
  210. }
  211. @CheckForNull
  212. ResourceDto getParentModuleByComponentId(Long componentId, DbSession session) {
  213. ResourceDto component = getResource(componentId, session);
  214. if (component != null) {
  215. Long rootId = component.getRootId();
  216. if (rootId != null) {
  217. return getParentModuleByComponentId(rootId, session);
  218. } else {
  219. return component;
  220. }
  221. }
  222. return null;
  223. }
  224. /**
  225. * Return the root project of a component.
  226. * Will return the component itself if it's already the root project
  227. * Can return null if the component that does exists.
  228. *
  229. * The implementation should rather use a new column already containing the root project, see https://jira.sonarsource.com/browse/SONAR-5188.
  230. */
  231. @CheckForNull
  232. public ResourceDto getRootProjectByComponentId(long componentId) {
  233. DbSession session = mybatis.openSession(false);
  234. try {
  235. ResourceDto component = getParentModuleByComponentId(componentId, session);
  236. Long rootId = component != null ? component.getRootId() : null;
  237. if (rootId != null) {
  238. return getParentModuleByComponentId(rootId, session);
  239. } else {
  240. return component;
  241. }
  242. } finally {
  243. MyBatis.closeQuietly(session);
  244. }
  245. }
  246. public List<Component> selectProjectsByQualifiers(Collection<String> qualifiers) {
  247. if (qualifiers.isEmpty()) {
  248. return Collections.emptyList();
  249. }
  250. SqlSession session = mybatis.openSession(false);
  251. try {
  252. return toComponents(session.getMapper(ResourceMapper.class).selectProjectsByQualifiers(qualifiers));
  253. } finally {
  254. MyBatis.closeQuietly(session);
  255. }
  256. }
  257. /**
  258. * Return enabled projects including not completed ones, ie without snapshots or without snapshot having islast=true
  259. */
  260. public List<Component> selectProjectsIncludingNotCompletedOnesByQualifiers(Collection<String> qualifiers) {
  261. if (qualifiers.isEmpty()) {
  262. return Collections.emptyList();
  263. }
  264. SqlSession session = mybatis.openSession(false);
  265. try {
  266. return toComponents(session.getMapper(ResourceMapper.class).selectProjectsIncludingNotCompletedOnesByQualifiers(qualifiers));
  267. } finally {
  268. MyBatis.closeQuietly(session);
  269. }
  270. }
  271. /**
  272. * Return ghosts projects :
  273. * - not enabled projects
  274. * - enabled projects without snapshot having islast=true
  275. * - enabled projects without snapshot
  276. */
  277. public List<Component> selectGhostsProjects(Collection<String> qualifiers) {
  278. if (qualifiers.isEmpty()) {
  279. return Collections.emptyList();
  280. }
  281. SqlSession session = mybatis.openSession(false);
  282. try {
  283. return toComponents(session.getMapper(ResourceMapper.class).selectGhostsProjects(qualifiers));
  284. } finally {
  285. MyBatis.closeQuietly(session);
  286. }
  287. }
  288. /**
  289. * Return provisioned projects = enabled projects without snapshot
  290. */
  291. public List<ResourceDto> selectProvisionedProjects(Collection<String> qualifiers) {
  292. if (qualifiers.isEmpty()) {
  293. return Collections.emptyList();
  294. }
  295. SqlSession session = mybatis.openSession(false);
  296. try {
  297. return session.getMapper(ResourceMapper.class).selectProvisionedProjects(qualifiers);
  298. } finally {
  299. MyBatis.closeQuietly(session);
  300. }
  301. }
  302. /**
  303. * Return provisioned project with given key
  304. */
  305. public ResourceDto selectProvisionedProject(DbSession session, String key) {
  306. return session.getMapper(ResourceMapper.class).selectProvisionedProject(key);
  307. }
  308. public ResourceDto selectProvisionedProject(String key) {
  309. DbSession session = mybatis.openSession(false);
  310. try {
  311. return selectProvisionedProject(session, key);
  312. } finally {
  313. MyBatis.closeQuietly(session);
  314. }
  315. }
  316. public static ComponentDto toComponent(ResourceDto resourceDto) {
  317. return new ComponentDto()
  318. .setId(resourceDto.getId())
  319. .setKey(resourceDto.getKey())
  320. .setPath(resourceDto.getPath())
  321. .setLongName(resourceDto.getLongName())
  322. .setName(resourceDto.getName())
  323. .setQualifier(resourceDto.getQualifier());
  324. }
  325. public static List<Component> toComponents(List<ResourceDto> resourceDto) {
  326. return newArrayList(Iterables.transform(resourceDto, new Function<ResourceDto, Component>() {
  327. @Override
  328. public Component apply(@Nullable ResourceDto resourceDto) {
  329. return resourceDto == null ? null : toComponent(resourceDto);
  330. }
  331. }));
  332. }
  333. public void insertUsingExistingSession(ResourceDto resourceDto, SqlSession session) {
  334. ResourceMapper resourceMapper = session.getMapper(ResourceMapper.class);
  335. resourceMapper.insert(resourceDto);
  336. }
  337. }