1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.ui.contentapp.detail;
35
36 import info.magnolia.ui.api.location.DefaultLocation;
37 import info.magnolia.ui.api.location.Location;
38
39 import org.apache.commons.lang3.StringUtils;
40
41
42
43
44
45
46
47
48
49
50
51
52 public class DetailLocation extends DefaultLocation {
53
54 private DetailView.ViewType viewType;
55 private String nodePath;
56 private String version;
57
58 private final static int NODE_PATH_PARAM_POSITION = 0;
59 private final static int VIEW_TYPE_PARAM_POSITION = 1;
60 private final static int VERSION_PARAM_POSITION = 2;
61
62 public DetailLocation(String appName, String subAppId, String parameter) {
63 super(LOCATION_TYPE_APP, appName, subAppId, parameter);
64
65 setNodePath(extractNodePath(parameter));
66 setViewType(extractViewType(parameter));
67 setVersion(extractVersion(parameter));
68 }
69
70 public DetailLocation(String appName, String subAppId, DetailView.ViewType viewType, String nodePath, String version) {
71 super(LOCATION_TYPE_APP, appName, subAppId);
72
73 setNodePath(nodePath);
74 setViewType(viewType);
75 setVersion(version);
76 updateParameter();
77 }
78
79 public String getNodePath() {
80 return unescapeSpecialCharacters(nodePath);
81 }
82
83
84
85
86 private void setNodePath(String nodePath) {
87 this.nodePath = (nodePath == null || nodePath.isEmpty()) ? "/" : escapeSpecialCharacters(nodePath);
88 }
89
90 public DetailView.ViewType getViewType() {
91 return viewType;
92 }
93
94 public void setViewType(DetailView.ViewType viewType) {
95 this.viewType = viewType;
96 }
97
98 public String getVersion() {
99 return unescapeSpecialCharacters(version);
100 }
101
102 public void setVersion(String version) {
103 this.version = escapeSpecialCharacters(version);
104 }
105
106 public boolean hasVersion() {
107 return StringUtils.isNotBlank(version);
108 }
109
110
111
112
113
114
115
116 private String extractNodePath(String parameter) {
117 return getParameter(parameter, NODE_PATH_PARAM_POSITION);
118 }
119
120
121
122
123
124
125
126 private DetailView.ViewType extractViewType(String parameter) {
127 String action = getParameter(parameter, VIEW_TYPE_PARAM_POSITION);
128 return DetailView.ViewType.fromString(action);
129 }
130
131
132
133
134
135
136
137 private String extractVersion(String parameter) {
138 return getParameter(parameter, VERSION_PARAM_POSITION);
139 }
140
141 protected String getParameter(String parameter, int position) {
142 String arguments[] = StringUtils.split(parameter, ':');
143 if (position <= arguments.length - 1) {
144 return arguments[position];
145 }
146 return "";
147 }
148
149 protected void updateParameter() {
150 StringBuilder sb = new StringBuilder();
151 sb.append(nodePath);
152 sb.append(":");
153 sb.append(viewType.getText());
154 if (StringUtils.isNotBlank(version)) {
155 sb.append(":");
156 sb.append(version);
157 }
158 super.setParameter(sb.toString());
159 }
160
161 public static DetailLocation wrap(Location location) {
162 return new DetailLocation(location.getAppName(), location.getSubAppId(), location.getParameter());
163 }
164
165 public void updateNodePath(String newNodePath) {
166 setNodePath(newNodePath);
167 updateParameter();
168 }
169
170 public void updateViewtype(DetailView.ViewType newViewType) {
171 setViewType(newViewType);
172 updateParameter();
173 }
174
175 public void updateVersion(String newVersion) {
176 setVersion(newVersion);
177 updateParameter();
178 }
179
180 }