]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/main/java/org/argeo/slc/core/structure/tree/TreeSPath.java
Progress on Web reporting
[gpl/argeo-slc.git] / org.argeo.slc.core / src / main / java / org / argeo / slc / core / structure / tree / TreeSPath.java
1 package org.argeo.slc.core.structure.tree;
2
3 import java.util.List;
4 import java.util.StringTokenizer;
5 import java.util.Vector;
6
7 import org.argeo.slc.core.SlcException;
8 import org.argeo.slc.core.structure.StructurePath;
9 import org.argeo.slc.core.structure.StructureRegistry;
10
11 /**
12 * Path for tree based <code>StructureRegistry</code> implementations.
13 */
14 public class TreeSPath implements StructurePath, Comparable<StructurePath> {
15 /** Default character to use a separator: /. */
16 public static Character DEFAULT_SEPARATOR = '/';
17
18 private TreeSPath parent;
19 private String name;
20 private Character separator = DEFAULT_SEPARATOR;
21
22 /** For ORM */
23 private Long tid;
24
25 public String getAsUniqueString() {
26 String parentStr = parent != null ? parent.getAsUniqueString() : "";
27 return parentStr + separator + name;
28 }
29
30 /** Sets all the required data from a string. */
31 public void setAsUniqueString(String str) {
32 TreeSPath twin = parseToCreatePath(str, getSeparator());
33 name = twin.name;
34 parent = twin.parent;
35 }
36
37 /** The separator actually used by this path. */
38 public Character getSeparator() {
39 return separator;
40 }
41
42 /** Gets the parent path. */
43 public TreeSPath getParent() {
44 return parent;
45 }
46
47 /** Gets the name part of the path. */
48 public String getName() {
49 return name;
50 }
51
52 /** Create a path without parent. */
53 public static TreeSPath createRootPath(String name) {
54 TreeSPath path = new TreeSPath();
55 path.parent = null;
56 path.name = name;
57 return path;
58 }
59
60 /** Create a child . */
61 public TreeSPath createChild(String name) {
62 if (name.indexOf(separator) > -1) {
63 throw new SlcException("Tree path name '" + name
64 + "' contains separator character " + separator);
65 }
66 TreeSPath path = new TreeSPath();
67 path.parent = this;
68 path.name = name;
69 return path;
70 }
71
72 /** Parses a string to a path. */
73 public static TreeSPath parseToCreatePath(String path) {
74 return parseToCreatePath(path, DEFAULT_SEPARATOR);
75 }
76
77 /** Parses a string to a path. */
78 public static TreeSPath parseToCreatePath(String path, Character separator) {
79 StringTokenizer st = new StringTokenizer(path, Character
80 .toString(separator));
81
82 TreeSPath currPath = null;
83 while (st.hasMoreTokens()) {
84 if (currPath == null) {// begin
85 currPath = createRootPath(st.nextToken());
86 } else {
87 currPath = currPath.createChild(st.nextToken());
88 }
89 }
90 return currPath;
91 }
92
93 /** Lists the children from a registry. */
94 public List<TreeSPath> listChildren(StructureRegistry registry) {
95 return listChildrenPaths(registry, this);
96 }
97
98 /** Lists the children from a given path from a registry. */
99 public static List<TreeSPath> listChildrenPaths(StructureRegistry registry,
100 TreeSPath path) {
101 List<TreeSPath> paths = new Vector<TreeSPath>();
102 List<StructurePath> allPaths = registry.listPaths();
103 for (StructurePath sPath : allPaths) {
104 TreeSPath pathT = (TreeSPath) sPath;
105 if (pathT.parent != null && pathT.parent.equals(path)) {
106 paths.add(pathT);
107 }
108 }
109 return paths;
110 }
111
112 /** Gets the root tree path of this path. */
113 public TreeSPath getRoot() {
114 TreeSPath root = this;
115 while (root.getParent() != null) {
116 root = root.getParent();
117 }
118 return root;
119 }
120
121 /** Depth of this path. */
122 public Integer getDepth() {
123 return depthImpl(this);
124 }
125
126 private static int depthImpl(TreeSPath path) {
127 if (path.getParent() == null) {
128 return 1;
129 } else {
130 return depthImpl(path.getParent()) + 1;
131 }
132 }
133
134 @Override
135 public String toString() {
136 return getAsUniqueString();
137 }
138
139 @Override
140 public boolean equals(Object obj) {
141 if (obj instanceof StructurePath) {
142 StructurePath path = (StructurePath) obj;
143 return getAsUniqueString().equals(path.getAsUniqueString());
144 }
145 return false;
146 }
147
148 public int compareTo(StructurePath o) {
149 return getAsUniqueString().compareTo(o.getAsUniqueString());
150 }
151
152 Long getTid() {
153 return tid;
154 }
155
156 void setTid(Long tid) {
157 this.tid = tid;
158 }
159
160 /** Sets the separator character to use. */
161 public void setSeparator(Character separator) {
162 this.separator = separator;
163 }
164
165 /** Sets the parent (for ORM). */
166 protected void setParent(TreeSPath parent) {
167 this.parent = parent;
168 }
169
170 /** Sets the name (for ORM). */
171 protected void setName(String name) {
172 this.name = name;
173 }
174
175 }