View Javadoc

1   /*
2   Copyright (C) 2007 Dirk Huenniger
3   
4   This library is free software; you can redistribute it and/or
5   modify it under the terms of the GNU Lesser General Public
6   License as published by the Free Software Foundation; either
7   version 2.1 of the License, or (at your option) any later version.
8   
9   This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  Lesser General Public License for more details.
13  
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   */
18  
19  package org.indi.objects;
20  
21  import java.io.Serializable;
22  import java.text.SimpleDateFormat;
23  import java.util.Collection;
24  import java.util.Date;
25  import java.util.LinkedList;
26  import java.util.SimpleTimeZone;
27  
28  public abstract class Vector<T extends org.indi.objects.Element> extends
29  	Object<T> implements Serializable {
30      /**
31       * 
32       */
33      private static final long serialVersionUID = 1L;
34      protected String timestamp = null;
35      protected String device = null;
36      protected State state = null;
37      protected Double timeout = null;
38      protected Permission permission = null;
39      public String name = null;
40      protected String label = null;
41      protected String group = null;
42  
43      abstract protected String getType();
44  
45      public Vector(String device, String name, String timestamp) {
46  	super(new LinkedList<T>());
47  	this.name = name;
48  	this.timestamp = timestamp;
49  	this.device = device;
50  	this.transferType = TransferType.New;
51      }
52  
53      public Vector(String device, String name, String label, String group,
54  	    State state, Permission permission, double timeout,
55  	    String timestamp, String message) {
56  	super(new LinkedList<T>());
57  	this.device = device;
58  	this.name = name;
59  	this.label = label;
60  	this.group = group;
61  	this.state = state;
62  	this.permission = permission;
63  	this.timeout = timeout;
64  	this.timestamp = timestamp;
65  	this.message = message;
66  	this.transferType = TransferType.Def;
67      }
68  
69      public Vector(String device, String name, State state, double timeout,
70  	    String timestamp, String message) {
71  	super(new LinkedList<T>());
72  	this.device = device;
73  	this.name = name;
74  	this.state = state;
75  	this.timeout = timeout;
76  	this.timestamp = timestamp;
77  	this.message = message;
78  	this.transferType = TransferType.Set;
79      }
80  
81      public SimpleDateFormat getDateFormat() {
82  	SimpleDateFormat dateFormat = new SimpleDateFormat(
83  		"yyyy-MM-dd'T'hh:mm:ss");
84  	dateFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));
85  	return dateFormat;
86      }
87  
88      public static String permissionToString(Permission permission) {
89  	return permission == Permission.ReadOnly ? "ro"
90  		: permission == Permission.WriteOnly ? "wo" :
91  		/* state==ReadWrite */"rw";
92      }
93  
94      public void add(T child) {
95  	this.children.add(child);
96      };
97  
98      public Collection<T> getChlidren() {
99  	return this.children;
100     };
101 
102     public String getStartTag(TransferType type, String message) {
103 	switch (type) {
104 	case Def:
105 	    return "<def" + this.getType() + " device=\"" + this.device
106 		    + "\" name=\"" + this.name + "\" label=\"" + this.label
107 		    + "\" group=\"" + this.group + "\" state=\""
108 		    + Light.stateToString(this.state) + "\" perm=\""
109 		    + permissionToString(this.permission) + "\" timeout=\""
110 		    + Double.toString(this.timeout) + "\" timestamp=\""
111 		    + getDateFormat().format(new Date())
112 		    + ((message == null) ? "" : "\" message=\"" + message)
113 		    + "\">";
114 	case Set:
115 	    return "<set" + this.getType() + " device=\"" + this.device
116 		    + "\" name=\"" + this.name + "\" state=\""
117 		    + Light.stateToString(this.state) + "\" timeout=\""
118 		    + Double.toString(this.timeout) + "\" timestamp=\""
119 		    + getDateFormat().format(new Date())
120 		    + ((message == null) ? "" : "\" message=\"" + message)
121 		    + "\">";
122 
123 	case New:
124 	    return "<new" + this.getType() + " device=\"" + this.device
125 		    + "\" name=\"" + this.name + "\">";
126 	case Del:
127 	    return "<delProperty " + " device=\"" + this.device + "\" name=\""
128 		    + this.name + "\" timestamp=\""
129 		    + getDateFormat().format(new Date())
130 		    + ((message == null) ? "" : "\" message=\"" + message)
131 		    + "\">";
132 	default:
133 	    throw new RuntimeException();
134 	}
135     }
136 
137     public String getEndTag(TransferType type) {
138 	switch (type) {
139 	case Def:
140 	    return "</def" + this.getType() + "> ";
141 	case Set:
142 	    return "</set" + this.getType() + "> ";
143 	case New:
144 	    return "</new" + this.getType() + "> ";
145 	case Del:
146 	    return "</delProperty> ";
147 	default:
148 	    throw new RuntimeException();
149 	}
150     }
151 
152     @Override
153     public String getXML(TransferType type, String message) {
154 
155 	String acu = getStartTag(type, message) + "\n";
156 	switch (type) {
157 	case Del:
158 	    break;
159 	default:
160 	    for (T o : this.children) {
161 		acu += "  " + o.getXML(type) + "\n";
162 	    }
163 	}
164 	return acu + getEndTag(type);
165     }
166 
167     public String getXML(TransferType type) {
168 	return getXML(type, null);
169     }
170 
171     public String getDevice() {
172 	return this.device;
173     }
174 
175     public String getName() {
176 	return this.name;
177     }
178 
179     public String getGroup() {
180 	return this.group;
181     }
182 
183     public void update(Vector<T> vector) {
184 	if (!(this.name.equals(vector.name))) {
185 	    throw new RuntimeException();
186 	}
187 	for (T o : vector.getChlidren()) {
188 	    for (T c : this.children) {
189 		if (c.name.equals(o.name)) {
190 		    c.setValue(o.value);
191 		}
192 	    }
193 	}
194     }
195 
196     public void setState(State state) {
197 	this.state = state;
198     }
199 
200     public State getState() {
201 	return this.state;
202     }
203 
204     @Override
205     public boolean oEquals(Object o) {
206 	if (o instanceof Vector) {
207 	    Vector v = (Vector) o;
208 	    return this.name.equals(v.name) && this.device.equals(v.device)
209 		    && getType().equals(v.getType());
210 	} else {
211 	    return false;
212 	}
213     }
214 
215     @Override
216     public int hashCode() {
217 	return this.name.hashCode();
218     }
219 
220 }