Source code: com/flexstor/common/data/ejb/search/MultiValueData.java
1 /*
2 * MultiValueData.java
3 *
4 * Copyright $Date: 2003/08/11 02:22:37 $ FLEXSTOR.net Inc.
5 *
6 * This work is licensed for use and distribution under license terms found at
7 * http://www.flexstor.org/license.html
8 *
9 */
10
11 package com.flexstor.common.data.ejb.search;
12
13 import com.flexstor.common.constants.FieldConstantsI;
14 import com.flexstor.common.data.ejb.Data;
15
16 /**
17 * This class supports a collection of fields from a search.
18 *
19 * @author Perry Hoekstra
20 * @version 1.0, 5/17/99
21 *
22 * @since FLEXSTOR.db 3.0
23 */
24 public class MultiValueData
25 extends Data
26 implements FieldConstantsI
27 {
28 // MKS macro expander
29 public final static String IDENTIFIER = "$Id: MultiValueData.java,v 1.3 2003/08/11 02:22:37 aleric Exp $";
30 static final long serialVersionUID = -5495087031089400336L;
31
32 /**
33 * The current data.
34 */
35 private String[] saData = null;
36
37 /**
38 * The current data ids (Used for lookup fields)
39 */
40 private long[] naIDs = null;
41
42 /**
43 * The original data -- required for discard functionality.
44 */
45 private String[] saBackupData = null;
46
47 /**
48 * The original ids -- required for discard functionality.
49 */
50 private long[] naBackupIds = null;
51
52 /**
53 * Modified flag.
54 */
55 private boolean bModified = false;
56
57
58 /**
59 * Default ValueNodeData constructor
60 */
61 public MultiValueData()
62 {
63 this ( new String[]{ DATA_NONE_VALUE }, new long[]{ DATA_NONE_ID } );
64 }
65
66 /**
67 * ValueNodeData constructor
68 * @param aStringArray value for a single field in a bucket record
69 */
70 public MultiValueData ( String[] saStringArray )
71 {
72 saData = saStringArray;
73 saBackupData = null;
74 bModified = false;
75
76 naIDs = new long[saStringArray.length];
77 for ( int i = 0; i < saStringArray.length; i++ )
78 naIDs[i] = DATA_NONE_ID;
79 }
80
81 /**
82 * ValueNodeData constructor
83 * @param String[]aStringArray value for a single field in a bucket record
84 * @param long[] aFieldIds - array of fields ids for lookup data
85 */
86 public MultiValueData ( String[] aStringArray, long[] fieldIDs )
87 {
88 saData = aStringArray;
89 saBackupData = null;
90 naIDs = fieldIDs;
91 naBackupIds = null;
92
93 bModified = false;
94 }
95
96 /**
97 * Clones this object.
98 */
99 public MultiValueData cloneObject ( )
100 {
101 return new MultiValueData ( (String[])getFieldData().clone(), (long[])getFieldIds().clone() );
102 }
103
104 /**
105 * Sets the field values.
106 */
107 public void setFieldData( String[] saData )
108 {
109 this.saData = saData;
110 }
111
112 /**
113 * Return the value for a single field in a bucket record
114 * @return the field data.
115 */
116 public String[] getFieldData()
117 {
118 return saData;
119 }
120
121 /**
122 * Sets the field values ids ( lookup record ids ).
123 */
124 public void setFieldIds( long[] naIDs )
125 {
126 this.naIDs = naIDs;
127 }
128
129 /**
130 * Return the value ids for a single field id in a bucket record
131 * @return long[] the field id
132 */
133 public long[] getFieldIds()
134 {
135 return naIDs;
136 }
137
138 /**
139 * Changes the data in this object and sets the modified flag.
140 * @param saNewData the new field data.
141 */
142 public void modifyData( String[] saNewData, long[] naNewIds )
143 {
144 // If the data has not been modified yet, then backup it up.
145 if ( !bModified )
146 {
147 saBackupData = saData;
148 naBackupIds = naIDs;
149
150 bModified = true;
151 }
152
153 saData = saNewData;
154 naIDs = naNewIds;
155 }
156
157 /**
158 * Restores the orginal data and unsets the modified flag.
159 * This method is required in order to do a refresh.
160 */
161 public void restoreData()
162 {
163 // If the data has been modified, then restore the orginal data.
164 if ( bModified )
165 {
166 saData = saBackupData;
167 saBackupData = null;
168 naIDs = naBackupIds;
169 naBackupIds = null;
170
171 bModified = false;
172 }
173 }
174
175 /**
176 * Resets this object so that the current data becomes the orginal data, and
177 * the modified flag is set to false.
178 */
179 public void resetData()
180 {
181 saBackupData = null;
182 naBackupIds = null;
183
184 bModified = false;
185 }
186
187 /**
188 * Returns true if the data has been modified.
189 * @return the modified state
190 */
191 public boolean isModified ( )
192 {
193 return bModified;
194 }
195
196 /**
197 * Sets modified flag.
198 * @param bModified the new modified state
199 */
200 public void setModified ( boolean bModified )
201 {
202 this.bModified = bModified;
203 }
204
205 /**
206 * Return the contents of the instance as a String. This method is strictly for use by testing classes
207 * and should not be called by business objects
208 *
209 * @return java.lang.String - String representation of BucketSkeletonData contents
210 *
211 */
212 public String toString ( )
213 {
214 String t_lineSeparator = System.getProperty("line.separator", "\n");
215
216 StringBuffer t_valueNodeDataContents = new StringBuffer(t_lineSeparator);
217
218 t_valueNodeDataContents.append("Class: ValueNodeData");
219
220 int t_nodeSize = saData.length;
221
222 for (int idx = 0; idx < t_nodeSize; idx++)
223 {
224 t_valueNodeDataContents.append(t_lineSeparator);
225 t_valueNodeDataContents.append("Node");
226 t_valueNodeDataContents.append(Integer.toString(idx));
227 t_valueNodeDataContents.append(": ");
228 t_valueNodeDataContents.append(saData[idx]);
229 t_valueNodeDataContents.append(" ^ ");
230 t_valueNodeDataContents.append(naIDs[idx]);
231 }
232
233 t_valueNodeDataContents.append(t_lineSeparator);
234
235 return t_valueNodeDataContents.toString();
236 }
237
238 public boolean equals( MultiValueData mvd )
239 {
240 if ( mvd.getFieldData().length != getFieldData().length || mvd.getFieldIds().length != getFieldIds().length )
241 return false;
242
243 for ( int i = 0; i < mvd.getFieldData().length; i++ )
244 {
245 if ( !mvd.getFieldData()[i].equals( getFieldData()[i] ) )
246 return false;
247 }
248
249 for ( int i = 0; i < mvd.getFieldIds().length; i++ )
250 {
251 if ( mvd.getFieldIds()[i] != getFieldIds()[i] )
252 return false;
253 }
254
255 return true;
256 }
257
258 } // end of class