Implement DB based version search
[smart-dao.git] / smart-version / smart-version-jgit-impl / src / main / java / com / smartitengineering / version / impl / jgit / JGitVersionControlDao.java
blob985fbff56deea0f9dcb6531a4b1b599dbacfee5a
1 /*
2 * This is a common dao with basic CRUD operations and is not limited to any
3 * persistent layer implementation
4 *
5 * Copyright (C) 2008 Imran M Yousuf (imyousuf@smartitengineering.com)
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 * This library 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 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 package com.smartitengineering.version.impl.jgit;
21 import com.smartitengineering.dao.common.queryparam.QueryParameter;
22 import com.smartitengineering.version.api.Commit;
23 import com.smartitengineering.version.api.Resource;
24 import com.smartitengineering.version.api.Revision;
25 import com.smartitengineering.version.api.VersionedResource;
26 import com.smartitengineering.version.api.dao.VersionControlDao;
27 import com.smartitengineering.version.api.dao.WriteStatus;
28 import com.smartitengineering.version.api.dao.WriterCallback;
29 import com.smartitengineering.version.api.factory.VersionAPI;
30 import com.smartitengineering.version.impl.jgit.service.MetaRCSService;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.Map;
34 import java.util.Set;
35 import org.apache.commons.lang.StringUtils;
37 /**
39 * @author imyousuf
41 public class JGitVersionControlDao
42 implements VersionControlDao {
44 private VersionControlDao jGitImpl;
45 private JGitDaoExtension jGitExtension;
46 private MetaRCSService jGitService;
48 public void store(Commit commit,
49 final WriterCallback callback) {
50 final Status myStatus = new Status();
51 try {
52 jGitImpl.store(commit, new WriterCallback() {
54 public void handle(Commit commit,
55 WriteStatus status,
56 String comment,
57 Throwable error) {
58 myStatus.myStatus = status;
59 myStatus.myComment = comment;
60 myStatus.myError = error;
61 if (status == WriteStatus.STORE_PASS) {
62 try {
63 jGitService.saveResources(commit);
65 catch (Throwable ex) {
66 myStatus.myStatus = WriteStatus.STORE_FAIL;
67 myStatus.myComment = ex.getMessage();
68 myStatus.myError = ex;
70 finally {
71 if (callback != null) {
72 callback.handle(commit, myStatus.myStatus,
73 myStatus.myComment,
74 myStatus.myError);
79 });
81 catch (Throwable ex) {
82 ex.printStackTrace();
83 myStatus.myStatus = WriteStatus.STORE_FAIL;
84 myStatus.myComment = ex.getMessage();
85 myStatus.myError = ex;
86 if (callback != null) {
87 callback.handle(commit, myStatus.myStatus, myStatus.myComment,
88 myStatus.myError);
93 private class Status {
95 public WriteStatus myStatus;
96 public String myComment;
97 public Throwable myError;
100 public void remove(Commit commit,
101 final WriterCallback callback) {
102 final Status myStatus = new Status();
103 try {
104 jGitImpl.remove(commit, new WriterCallback() {
106 public void handle(Commit commit,
107 WriteStatus status,
108 String comment,
109 Throwable error) {
110 myStatus.myStatus = status;
111 myStatus.myComment = comment;
112 myStatus.myError = error;
113 if (status == WriteStatus.STORE_PASS) {
114 try {
115 jGitService.deleteResources(commit);
117 catch (Throwable ex) {
118 myStatus.myStatus = WriteStatus.STORE_FAIL;
119 myStatus.myComment = ex.getMessage();
120 myStatus.myError = ex;
122 finally {
123 if (callback != null) {
124 callback.handle(commit, myStatus.myStatus,
125 myStatus.myComment,
126 myStatus.myError);
133 catch (Throwable ex) {
134 ex.printStackTrace();
135 myStatus.myStatus = WriteStatus.STORE_FAIL;
136 myStatus.myComment = ex.getMessage();
137 myStatus.myError = ex;
138 if (callback != null) {
139 callback.handle(commit, myStatus.myStatus, myStatus.myComment,
140 myStatus.myError);
145 public VersionedResource getVersionedResource(String resourceId) {
146 String trimmedResourceId = VersionAPI.trimToPropertResourceId(
147 resourceId);
148 if (StringUtils.isBlank(trimmedResourceId)) {
149 throw new IllegalArgumentException("Invalid resource id!");
151 String[] versions = jGitService.getVersionsForResource(trimmedResourceId);
152 try {
153 Map<String, byte[]> objectMap =
154 jGitExtension.readBlobObjects(versions);
155 Revision[] revisions = new Revision[objectMap.size()];
156 int i = 0;
157 for (String revisionId : versions) {
158 String content = new String(objectMap.get(revisionId));
159 revisions[i++] = VersionAPI.createRevision(VersionAPI.
160 createResource(trimmedResourceId, content), revisionId);
162 return VersionAPI.createVersionedResource(Arrays.asList(revisions));
164 catch (Exception ex) {
165 ex.printStackTrace();
166 throw new RuntimeException(ex);
170 public Resource getResource(String resourceId) {
171 try {
172 String trimmedResourceId = VersionAPI.trimToPropertResourceId(
173 resourceId);
174 if (StringUtils.isBlank(trimmedResourceId)) {
175 throw new IllegalArgumentException("Invalid resource id!");
177 final String headVersionForResource =
178 jGitService.getHeadVersionForResource(trimmedResourceId);
179 final byte[] objectBytes =
180 jGitExtension.readObject(headVersionForResource);
181 if(objectBytes == null || objectBytes.length <= 0) {
182 throw new IllegalArgumentException("Resource doesn't exist!");
184 return VersionAPI.createResource(trimmedResourceId,
185 new String(objectBytes));
187 catch (Exception ex) {
188 ex.printStackTrace();
189 throw new RuntimeException(ex);
193 public Resource getResourceByRevision(String revisionId,
194 String resourceId) {
195 try {
196 String trimmedResourceId = VersionAPI.trimToPropertResourceId(
197 resourceId);
198 if (StringUtils.isBlank(trimmedResourceId)) {
199 throw new IllegalArgumentException("Invalid resource id!");
201 return VersionAPI.createResource(trimmedResourceId,
202 new String(jGitExtension.readObject(revisionId)));
204 catch (Exception ex) {
205 ex.printStackTrace();
206 throw new RuntimeException(ex);
211 * Only use property param from SearchProperty that start with 'COMMIT'.
212 * @see VersionControlDao#searchForCommits(java.util.Collection)
213 * @see SearchParam#getPropertyName()
214 * @see SearchProperties
216 public Collection<Commit> searchForCommits(
217 Collection<QueryParameter> parameters) {
218 Set<Commit> commits = jGitService.searchForCommits(parameters);
219 return commits;
223 * Only use property param from SearchProperty that start with 'REVISION'.
224 * @see VersionControlDao#searchForRevisions(java.util.Collection)
225 * @see SearchParam#getPropertyName()
226 * @see SearchProperties
228 public Collection<Revision> searchForRevisions(
229 Collection<QueryParameter> parameters) {
230 Set<Revision> revisions = jGitService.searchForRevisions(parameters);
231 return revisions;
234 public JGitDaoExtension getJGitExtension() {
235 return jGitExtension;
238 public void setJGitExtension(JGitDaoExtension jGitExtension) {
239 this.jGitExtension = jGitExtension;
242 public VersionControlDao getJGitImpl() {
243 return jGitImpl;
246 public void setJGitImpl(VersionControlDao jGitImpl) {
247 this.jGitImpl = jGitImpl;
250 public MetaRCSService getJGitService() {
251 return jGitService;
254 public void setJGitService(MetaRCSService jGitService) {
255 this.jGitService = jGitService;