Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Position |
|
| 2.25;2.25 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to you under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | ||
18 | package org.apache.shale.dialog.basic; | |
19 | ||
20 | import java.io.Serializable; | |
21 | import java.util.Map; | |
22 | ||
23 | import javax.faces.context.FacesContext; | |
24 | ||
25 | import org.apache.shale.dialog.basic.model.Dialog; | |
26 | import org.apache.shale.dialog.basic.model.State; | |
27 | ||
28 | /** | |
29 | * <p>JavaBean that represents the current {@link State} within | |
30 | * a {@link Dialog}.</p> | |
31 | */ | |
32 | class Position implements Serializable { | |
33 | ||
34 | ||
35 | // ------------------------------------------------------------ Constructors | |
36 | ||
37 | ||
38 | /** | |
39 | * Serial version UID. | |
40 | */ | |
41 | private static final long serialVersionUID = -1600336297791202073L; | |
42 | ||
43 | ||
44 | /** | |
45 | * <p>Construct a new {@link Position} representing the specified | |
46 | * {@link State} for the specified {@link Dialog}, and associated | |
47 | * with the specified <code>data</code> instance.</p> | |
48 | * | |
49 | * @param dialog {@link Dialog} to be recorded | |
50 | * @param state {@link State} to be recorded | |
51 | * @param data Data instance to be recorded | |
52 | */ | |
53 | 0 | Position(Dialog dialog, State state, java.lang.Object data) { |
54 | 0 | if (dialog == null) { |
55 | 0 | throw new IllegalArgumentException("Dialog cannot be null"); |
56 | } | |
57 | 0 | setDialog(dialog); |
58 | 0 | setState(state); |
59 | 0 | setData(data); |
60 | 0 | } |
61 | ||
62 | ||
63 | // ------------------------------------------------------ Instance Variables | |
64 | ||
65 | ||
66 | /** | |
67 | * <p>The data instance for the dialog execution represeented by this | |
68 | * {@link Position}.</p> | |
69 | */ | |
70 | 0 | private Object data = null; |
71 | ||
72 | ||
73 | /** | |
74 | * <p>The {@link Dialog} within which this {@link Position} is reported. | |
75 | * This value is transient, and may need to be regenerated.</p> | |
76 | */ | |
77 | 0 | private transient Dialog dialog = null; |
78 | ||
79 | ||
80 | /** | |
81 | * <p>The name of the {@link Dialog} within which this {@link Position} | |
82 | * is reported.</p> | |
83 | */ | |
84 | 0 | private String dialogName = null; |
85 | ||
86 | ||
87 | /** | |
88 | * <p>The {@link State} that represents the current position within the | |
89 | * {@link Dialog} that is being executed. This value is transient, and | |
90 | * may need to be regenerated.</p> | |
91 | */ | |
92 | 0 | private transient State state = null; |
93 | ||
94 | ||
95 | /** | |
96 | * <p>The name of the {@link State} that represents the current position | |
97 | * within the {@link Dialog} that is being executed.</p> | |
98 | */ | |
99 | 0 | private String stateName = null; |
100 | ||
101 | ||
102 | // ------------------------------------------------------ Package Properties | |
103 | ||
104 | ||
105 | /** | |
106 | * <p>Return the data object associated with this dialog execution.</p> | |
107 | * | |
108 | * @return The associated data object for this dialog instance | |
109 | */ | |
110 | Object getData() { | |
111 | 0 | return this.data; |
112 | } | |
113 | ||
114 | ||
115 | /** | |
116 | * <p>Set the data object associated with this dialog execution.</p> | |
117 | * | |
118 | * @param data The new data object | |
119 | */ | |
120 | void setData(Object data) { | |
121 | 0 | this.data = data; |
122 | 0 | } |
123 | ||
124 | ||
125 | /** | |
126 | * <p>Return the {@link Dialog} whose execution is tracked by this | |
127 | * {@link Position}.</p> | |
128 | * | |
129 | * @return The {@link Dialog} being tracked by this {@link Position} | |
130 | * instance | |
131 | */ | |
132 | Dialog getDialog() { | |
133 | 0 | if (this.dialog != null) { |
134 | 0 | return this.dialog; |
135 | } | |
136 | 0 | Map map = (Map) |
137 | FacesContext.getCurrentInstance().getExternalContext(). | |
138 | getApplicationMap().get(Globals.DIALOGS); | |
139 | 0 | this.dialog = (Dialog) map.get(this.dialogName); |
140 | 0 | return this.dialog; |
141 | } | |
142 | ||
143 | ||
144 | /** | |
145 | * <p>Set the {@link Dialog} whose execution is being tracked by this | |
146 | * {@link Position}.</p> | |
147 | * | |
148 | * @param dialog The {@link Dialog} instance being tracked | |
149 | */ | |
150 | private void setDialog(Dialog dialog) { | |
151 | 0 | this.dialog = dialog; |
152 | 0 | this.dialogName = dialog.getName(); |
153 | 0 | } |
154 | ||
155 | ||
156 | /** | |
157 | * <p>Return the {@link State} representing the current execution position | |
158 | * within the owning {@link Dialog}.</p> | |
159 | * | |
160 | * @return The current {@link State} within the owning {@link Dialog} | |
161 | */ | |
162 | State getState() { | |
163 | 0 | if (this.state == null) { |
164 | 0 | Dialog dialog = getDialog(); |
165 | 0 | if (dialog == null) { |
166 | 0 | return null; |
167 | } | |
168 | 0 | this.state = dialog.findState(this.stateName); |
169 | } | |
170 | 0 | return this.state; |
171 | } | |
172 | ||
173 | ||
174 | /** | |
175 | * <p>Set the {@link State} representing the current execution position | |
176 | * within the owning {@link Dialog}.</p> | |
177 | * | |
178 | * @param state The new {@link State} | |
179 | */ | |
180 | void setState(State state) { | |
181 | 0 | if (state == null) { |
182 | 0 | this.state = null; |
183 | 0 | this.stateName = null; |
184 | 0 | } else { |
185 | 0 | this.state = state; |
186 | 0 | this.stateName = state.getName(); |
187 | } | |
188 | 0 | } |
189 | ||
190 | ||
191 | // ---------------------------------------------------------- Public Methods | |
192 | ||
193 | ||
194 | /** | |
195 | * <p>Return a String representation of this object.</p> | |
196 | * | |
197 | * @return The String representation of this {@link Position} instance | |
198 | */ | |
199 | public String toString() { | |
200 | 0 | if (getState() == null) { |
201 | 0 | return "Position[dialog=" + getDialog().getName() |
202 | + ",data=" + getData() + "]"; | |
203 | } else { | |
204 | 0 | return "Position[dialog=" + getDialog().getName() |
205 | + ",state=" + getState().getName() | |
206 | + ",data=" + getData() + "]"; | |
207 | } | |
208 | } | |
209 | ||
210 | ||
211 | } |