View Javadoc

1   /***
2    * Copyright (C) 2008 Jeremy Thomerson (jthomerson@users.sourceforge.net)
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.sf.vcaperture.tool.listeners;
18  
19  import java.util.Arrays;
20  import java.util.Collection;
21  import java.util.LinkedHashSet;
22  import java.util.Set;
23  
24  import net.sf.vcaperture.model.AbstractRepository;
25  import net.sf.vcaperture.model.ProcessedRepository;
26  import net.sf.vcaperture.model.RepoFile;
27  import net.sf.vcaperture.model.RepoFileRevision;
28  import net.sf.vcaperture.model.Revision;
29  import net.sf.vcaperture.services.ILocalStorageService;
30  import net.sf.vcaperture.services.IRepositoryService;
31  
32  /***
33   * This tool stores everything from the repository into a local database.
34   * 
35   * @author Jeremy Thomerson (jthomerson@users.sourceforge.net)
36   * @see ProcessedRepository
37   */
38  public class LocalStorageTool extends AbstractToolListener {
39  
40  	public static final int DEFAULT_MAX_REVISION_PER_BATCH = 50;
41  	
42  	private ILocalStorageService mStorageService;
43  	private int mMaxRevisionsPerBatch = DEFAULT_MAX_REVISION_PER_BATCH;
44  
45  	@Override
46  	public boolean processRepository(IRepositoryService repositoryService, AbstractRepository repo) {
47  		super.processRepository(repositoryService, repo);
48  
49  		String latestStoredRevision = mStorageService.getLatestStoredRevision(repo);
50  		getLogger().debug("Latest already-stored revision: " + latestStoredRevision);
51  		if (latestStoredRevision.equals(repo.getLatestRevision())) {
52  			return true;
53  		}
54  		Collection<Revision> revisions = repo.getRevisions(latestStoredRevision, mMaxRevisionsPerBatch);
55  		for (Revision rev : revisions) {
56  			getLogger().info("Storing revision: " + rev.getName());
57  			for (RepoFileRevision rfr : rev.getFiles()) {
58  				getLogger().debug("Storing: " + rfr.getRelativePath() + " [" + rev.getName() + "]");
59  				RepoFile rf = mStorageService.getFile(repo, rfr.getRelativePath());
60  				Set<String> revs = rf == null ? new LinkedHashSet<String>() : new LinkedHashSet<String>(Arrays.asList(rf.getRevisions()));
61  				revs.add(rfr.getRevision());
62  				rfr.setRevisions(revs.toArray(new String[revs.size()]));
63  				mStorageService.saveFileRevision(repo, rfr);
64  			}
65  			mStorageService.saveRevision(repo, rev);
66  		}
67  		// TODO: this code is  shared with CodeSearchTool - both should subclass a MaxRevisionsTool
68  		// not complete if we had exactly mMaxRevisionsPerBatch (there are probably more)
69  		// TRUE, you could have EXACTLY mMaxRevisionsPerBatch, but if so, it doesn't hurt to run again
70  		return (revisions.size() >= mMaxRevisionsPerBatch);
71  	}
72  
73  	public void setStorageService(ILocalStorageService storageService) {
74  		mStorageService = storageService;
75  	}
76  
77  	public ILocalStorageService getStorageService() {
78  		return mStorageService;
79  	}
80  
81  	public int getMaxRevisionsPerBatch() {
82      	return mMaxRevisionsPerBatch;
83      }
84  
85  	public void setMaxRevisionsPerBatch(int maxRevisionsPerBatch) {
86      	mMaxRevisionsPerBatch = maxRevisionsPerBatch;
87      }
88  
89  }