Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/AbstractStaticStateArtifact.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-artifacts/src/main/java/org/dive4elements/river/artifacts/AbstractStaticStateArtifact.java@bd047b71ab37 |
children | 4897a58c8746 |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.artifacts; | |
2 | |
3 import java.util.ArrayList; | |
4 import java.util.Collection; | |
5 import java.util.List; | |
6 | |
7 import org.apache.log4j.Logger; | |
8 | |
9 import org.w3c.dom.Document; | |
10 import org.w3c.dom.Element; | |
11 | |
12 import org.dive4elements.artifacts.CallContext; | |
13 import org.dive4elements.artifacts.ArtifactNamespaceContext; | |
14 | |
15 import org.dive4elements.artifacts.common.utils.XMLUtils; | |
16 import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator; | |
17 | |
18 import org.dive4elements.artifactdatabase.ProtocolUtils; | |
19 import org.dive4elements.artifactdatabase.data.StateData; | |
20 import org.dive4elements.artifactdatabase.state.State; | |
21 | |
22 import org.dive4elements.river.artifacts.states.StaticState; | |
23 | |
24 /** | |
25 * A abstract baseclass for Artifacts which are using only one static state. | |
26 * | |
27 * This class is intended to be used without the config/stateengine to generate | |
28 * the static state. | |
29 * | |
30 * @author <a href="mailto:bjoern.ricks@intevation.de">Björn Ricks</a> | |
31 */ | |
32 public abstract class AbstractStaticStateArtifact extends StaticFLYSArtifact { | |
33 | |
34 private transient StaticState staticstate; | |
35 | |
36 private static final Logger logger = | |
37 Logger.getLogger(AbstractStaticStateArtifact.class); | |
38 | |
39 /** | |
40 * Get a list containing the one and only State. | |
41 * @param context ignored. | |
42 * @return list with one and only state. | |
43 */ | |
44 @Override | |
45 protected List<State> getStates(Object context) { | |
46 ArrayList<State> states = new ArrayList<State>(); | |
47 states.add(getStaticState()); | |
48 return states; | |
49 } | |
50 | |
51 | |
52 /** | |
53 * Get the "current" state. | |
54 * @param cc ignored. | |
55 * @return always the set static state. | |
56 */ | |
57 @Override | |
58 public State getCurrentState(Object cc) { | |
59 return getStaticState(); | |
60 } | |
61 | |
62 /** | |
63 * A child class must override this method to set its static state | |
64 */ | |
65 protected abstract void initStaticState(); | |
66 | |
67 protected void setStaticState(StaticState state) { | |
68 this.staticstate = state; | |
69 } | |
70 | |
71 protected StaticState getStaticState() { | |
72 if (staticstate == null) { | |
73 initStaticState(); | |
74 } | |
75 return staticstate; | |
76 } | |
77 | |
78 /** | |
79 * Get the state. | |
80 * @param context ignored. | |
81 * @param stateID ignored. | |
82 * @return the state. | |
83 */ | |
84 @Override | |
85 protected State getState(Object context, String stateID) { | |
86 return getStaticState(); | |
87 } | |
88 | |
89 @Override | |
90 public Document describe(Document data, CallContext cc) { | |
91 logger.debug("Describe artifact: " + identifier()); | |
92 | |
93 Document desc = XMLUtils.newDocument(); | |
94 | |
95 ElementCreator creator = new ElementCreator( | |
96 desc, | |
97 ArtifactNamespaceContext.NAMESPACE_URI, | |
98 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
99 | |
100 Element root = ProtocolUtils.createRootNode(creator); | |
101 desc.appendChild(root); | |
102 | |
103 Element name = ProtocolUtils.createArtNode( | |
104 creator, "name", | |
105 new String[] { "value" }, | |
106 new String[] { getName() }); | |
107 | |
108 root.appendChild(name); | |
109 root.appendChild(createOutputModes(cc, desc, creator)); | |
110 | |
111 ProtocolUtils.appendDescribeHeader(creator, root, identifier(), hash()); | |
112 | |
113 // Add the data to an anonymous state. | |
114 Collection<StateData> datas = getAllData(); | |
115 if (datas.size() > 0) { | |
116 Element ui = creator.create("ui"); | |
117 Element staticE = creator.create("static"); | |
118 | |
119 StaticState current = getStaticState(); | |
120 Element state = current.describeStatic(this, desc, root, cc, null); | |
121 staticE.appendChild(state); | |
122 | |
123 for (StateData dataItem : datas) { | |
124 Element itemelent = creator.create("data"); | |
125 creator.addAttr(itemelent, "name", dataItem.getName(), true); | |
126 creator.addAttr(itemelent, "type", dataItem.getType(), true); | |
127 state.appendChild(itemelent); | |
128 Element valuement = creator.create("item"); | |
129 creator.addAttr(valuement, "label", dataItem.getDescription(), true); | |
130 creator.addAttr(valuement, "value", dataItem.getValue().toString(), true); | |
131 itemelent.appendChild(valuement); | |
132 } | |
133 | |
134 ui.appendChild(staticE); | |
135 root.appendChild(ui); | |
136 } | |
137 | |
138 return desc; | |
139 } | |
140 } |