]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc/src/main/java/org/argeo/slc/core/structure/tree/TreeSPath.java
Improve reporting
[gpl/argeo-slc.git] / org.argeo.slc / 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 /** Parses a string to a path. */
72 public static TreeSPath parseToCreatePath(String path) {
73 return parseToCreatePath(path, DEFAULT_SEPARATOR);
74 }
75
76 /** Parses a string to a path. */
77 public static TreeSPath parseToCreatePath(String path, Character separator) {
78 StringTokenizer st = new StringTokenizer(path, Character
79 .toString(separator));
80
81 TreeSPath currPath = null;
82 while (st.hasMoreTokens()) {
83 if (currPath == null) {// begin
84 currPath = createRootPath(st.nextToken());
85 } else {
86 currPath = currPath.createChild(st.nextToken());
87 }
88 }
89 return currPath;
90 }
91
92 /** Lists the children from a registry. */
93 public List<TreeSPath> listChildren(StructureRegistry registry) {
94 return listChildrenPaths(registry, this);
95 }
96
97 /** Lists the children from a given path from a registry. */
98 public static List<TreeSPath> listChildrenPaths(StructureRegistry registry,
99 TreeSPath path) {
100 List<TreeSPath> paths = new Vector<TreeSPath>();
101 List<StructurePath> allPaths = registry.listPaths();
102 for (StructurePath sPath : allPaths) {
103 TreeSPath pathT = (TreeSPath) sPath;
104 if (pathT.parent != null && pathT.parent.equals(path)) {
105 paths.add(pathT);
106 }
107 }
108 return paths;
109 }
110
111 /** Gets the root tree path of this path. */
112 public TreeSPath getRoot() {
113 TreeSPath root = this;
114 while (root.getParent() != null) {
115 root = root.getParent();
116 }
117 return root;
118 }
119
120 @Override
121 public String toString() {
122 return getAsUniqueString();
123 }
124
125 @Override
126 public boolean equals(Object obj) {
127 if (obj instanceof StructurePath) {
128 StructurePath path = (StructurePath) obj;
129 return getAsUniqueString().equals(path.getAsUniqueString());
130 }
131 return false;
132 }
133
134 public int compareTo(StructurePath o) {
135 return getAsUniqueString().compareTo(o.getAsUniqueString());
136 }
137
138 Long getTid() {
139 return tid;
140 }
141
142 void setTid(Long tid) {
143 this.tid = tid;
144 }
145
146 /** Sets the separator character to use. */
147 public void setSeparator(Character separator) {
148 this.separator = separator;
149 }
150
151 /** Sets the parent (for ORM). */
152 protected void setParent(TreeSPath parent) {
153 this.parent = parent;
154 }
155
156 /** Sets the name (for ORM). */
157 protected void setName(String name) {
158 this.name = name;
159 }
160
161 }