change browse api in order to build a tree base file structure

This commit is contained in:
Sebastian Sdorra
2018-10-08 14:38:27 +02:00
parent 27573bc4cf
commit e5a3cbb50e
5 changed files with 118 additions and 224 deletions

View File

@@ -1,19 +1,19 @@
/**
* Copyright (c) 2010, Sebastian Sdorra
* All rights reserved.
*
* <p>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* <p>
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of SCM-Manager; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
* <p>
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@@ -24,13 +24,11 @@
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* <p>
* http://bitbucket.org/sdorra/scm-manager
*
*/
package sonia.scm.repository;
//~--- non-JDK imports --------------------------------------------------------
@@ -40,12 +38,8 @@ import com.google.common.base.Objects;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
//~--- JDK imports ------------------------------------------------------------
@@ -56,224 +50,56 @@ import java.util.List;
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "browser-result")
public class BrowserResult implements Iterable<FileObject>, Serializable
{
public class BrowserResult implements Serializable {
/** Field description */
private static final long serialVersionUID = 2818662048045182761L;
private String revision;
private FileObject file;
//~--- constructors ---------------------------------------------------------
/**
* Constructs ...
*
*/
public BrowserResult() {}
/**
* Constructs ...
*
*
* @param revision
* @param tag
* @param branch
* @param files
*/
public BrowserResult(String revision, String tag, String branch,
List<FileObject> files)
{
this.revision = revision;
this.tag = tag;
this.branch = branch;
this.files = files;
public BrowserResult() {
}
//~--- methods --------------------------------------------------------------
public BrowserResult(String revision, FileObject file) {
this.revision = revision;
this.file = file;
}
public String getRevision() {
return revision;
}
public FileObject getFile() {
return file;
}
/**
* {@inheritDoc}
*
*
* @param obj
*
* @return
*/
@Override
public boolean equals(Object obj)
{
if (obj == null)
{
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass())
{
if (getClass() != obj.getClass()) {
return false;
}
final BrowserResult other = (BrowserResult) obj;
return Objects.equal(revision, other.revision)
&& Objects.equal(tag, other.tag)
&& Objects.equal(branch, other.branch)
&& Objects.equal(files, other.files);
&& Objects.equal(file, other.file);
}
/**
* {@inheritDoc}
*
*
* @return
*/
@Override
public int hashCode()
{
return Objects.hashCode(revision, tag, branch, files);
public int hashCode() {
return Objects.hashCode(revision, file);
}
/**
* Method description
*
*
* @return
*/
@Override
public Iterator<FileObject> iterator()
{
Iterator<FileObject> it = null;
if (files != null)
{
it = files.iterator();
}
return it;
}
/**
* {@inheritDoc}
*
*
* @return
*/
@Override
public String toString()
{
//J-
public String toString() {
return MoreObjects.toStringHelper(this)
.add("revision", revision)
.add("tag", tag)
.add("branch", branch)
.add("files", files)
.toString();
//J+
.add("revision", revision)
.add("files", file)
.toString();
}
//~--- get methods ----------------------------------------------------------
/**
* Method description
*
*
* @return
*/
public String getBranch()
{
return branch;
}
/**
* Method description
*
*
* @return
*/
public List<FileObject> getFiles()
{
return files;
}
/**
* Method description
*
*
* @return
*/
public String getRevision()
{
return revision;
}
/**
* Method description
*
*
* @return
*/
public String getTag()
{
return tag;
}
//~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @param branch
*/
public void setBranch(String branch)
{
this.branch = branch;
}
/**
* Method description
*
*
* @param files
*/
public void setFiles(List<FileObject> files)
{
this.files = files;
}
/**
* Method description
*
*
* @param revision
*/
public void setRevision(String revision)
{
this.revision = revision;
}
/**
* Method description
*
*
* @param tag
*/
public void setTag(String tag)
{
this.tag = tag;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private String branch;
/** Field description */
@XmlElement(name = "file")
@XmlElementWrapper(name = "files")
private List<FileObject> files;
/** Field description */
private String revision;
/** Field description */
private String tag;
}

View File

@@ -37,6 +37,7 @@ package sonia.scm.repository;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.base.Strings;
import sonia.scm.LastModifiedAware;
import javax.xml.bind.annotation.XmlAccessType;
@@ -44,6 +45,8 @@ import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
//~--- JDK imports ------------------------------------------------------------
@@ -181,6 +184,22 @@ public class FileObject implements LastModifiedAware, Serializable
return path;
}
/**
* Returns the parent path of the file.
*
* @return parent path
*/
public String getParentPath() {
if (Strings.isNullOrEmpty(path)) {
return null;
}
int index = path.lastIndexOf('/');
if (index > 0) {
return path.substring(0, index);
}
return "";
}
/**
* Return sub repository informations or null if the file is not
* sub repository.
@@ -284,6 +303,22 @@ public class FileObject implements LastModifiedAware, Serializable
this.subRepository = subRepository;
}
public List<FileObject> getChildren() {
return children;
}
public void setChildren(List<FileObject> children) {
this.children = children;
}
public void addChild(FileObject child) {
this.children.add(child);
}
public boolean hasChildren() {
return !children.isEmpty();
}
//~--- fields ---------------------------------------------------------------
/** file description */
@@ -307,4 +342,6 @@ public class FileObject implements LastModifiedAware, Serializable
/** sub repository informations */
@XmlElement(name = "subrepository")
private SubRepository subRepository;
private List<FileObject> children = new ArrayList<>();
}

View File

@@ -161,11 +161,21 @@ public class PreProcessorUtil
{
if (logger.isTraceEnabled())
{
logger.trace("prepare browser result of repository {} for return",
repository.getName());
logger.trace("prepare browser result of repository {} for return", repository.getName());
}
handlePreProcessForIterable(repository, result,fileObjectPreProcessorFactorySet, fileObjectPreProcessorSet);
PreProcessorHandler<FileObject> handler = new PreProcessorHandler<>(fileObjectPreProcessorFactorySet, fileObjectPreProcessorSet, repository);
handlePreProcessorForFileObject(handler, result.getFile());
}
private void handlePreProcessorForFileObject(PreProcessorHandler<FileObject> handler, FileObject fileObject) {
if (fileObject.isDirectory()) {
for (FileObject child : fileObject.getChildren()) {
handlePreProcessorForFileObject(handler, child);
}
}
handler.callPreProcessorFactories(fileObject);
handler.callPreProcessors(fileObject);
}
/**

View File

@@ -42,7 +42,6 @@ import sonia.scm.cache.Cache;
import sonia.scm.cache.CacheManager;
import sonia.scm.repository.BrowserResult;
import sonia.scm.repository.FileObject;
import sonia.scm.repository.FileObjectNameComparator;
import sonia.scm.repository.PreProcessorUtil;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryCacheKey;
@@ -52,8 +51,6 @@ import sonia.scm.repository.spi.BrowseCommandRequest;
import java.io.IOException;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
//~--- JDK imports ------------------------------------------------------------
@@ -180,14 +177,6 @@ public final class BrowseCommandBuilder
if (!disablePreProcessors && (result != null))
{
preProcessorUtil.prepareForReturn(repository, result);
List<FileObject> fileObjects = result.getFiles();
if (fileObjects != null)
{
Collections.sort(fileObjects, FileObjectNameComparator.instance);
result.setFiles(fileObjects);
}
}
return result;

View File

@@ -0,0 +1,32 @@
package sonia.scm.repository;
import org.junit.Test;
import static org.junit.Assert.*;
public class FileObjectTest {
@Test
public void getParentPath() {
FileObject file = create("a/b/c");
assertEquals("a/b", file.getParentPath());
}
@Test
public void getParentPathWithoutParent() {
FileObject file = create("a");
assertEquals("", file.getParentPath());
}
@Test
public void getParentPathOfRoot() {
FileObject file = create("");
assertNull(file.getParentPath());
}
private FileObject create(String path) {
FileObject file = new FileObject();
file.setPath(path);
return file;
}
}