1 | /** |
2 | * Copyright 2005-2011 Steve McDuff d-duff@users.sourceforge.net |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | package org.deduced.viewer.web.serializer; |
17 | |
18 | import java.io.Serializable; |
19 | |
20 | import org.deduced.AbstractPropertyCollection; |
21 | import org.deduced.DeducedUtilities; |
22 | import org.deduced.PropertyCollection; |
23 | import org.deduced.viewer.web.shared.Model; |
24 | |
25 | /** |
26 | * Model Serializer |
27 | * |
28 | * @author Steve McDuff |
29 | * |
30 | */ |
31 | public abstract class ModelSerializer implements WebSerializer |
32 | { |
33 | /** |
34 | * fill the Model |
35 | * |
36 | * @param object collection to serialize |
37 | * @param model the model to fill |
38 | */ |
39 | public static void fillModel( |
40 | PropertyCollection<?, ?> object, Model model) |
41 | { |
42 | setID(object, model); |
43 | } |
44 | |
45 | /** |
46 | * set the ID of a collection on it's serialized form |
47 | * |
48 | * @param collection collection to serialize |
49 | * @param model the model on which to set the ID |
50 | */ |
51 | public static void setID( |
52 | PropertyCollection<?, ?> collection, Model model) |
53 | { |
54 | model.setId(getID(collection)); |
55 | } |
56 | |
57 | /** |
58 | * get a collection ID in string form |
59 | * |
60 | * @param collection the collection from which to fetch the ID |
61 | * @return the collection ID string |
62 | */ |
63 | public static String getID( |
64 | PropertyCollection<?, ?> collection) |
65 | { |
66 | return AbstractPropertyCollection.getInstanceIdentityKey(collection) |
67 | .toString(); |
68 | } |
69 | |
70 | /** |
71 | * (non-JSDoc) |
72 | * |
73 | * @see org.deduced.viewer.web.serializer.WebSerializer#serialize(org.deduced.PropertyCollection, |
74 | * org.deduced.viewer.web.serializer.MasterWebSerializer) |
75 | */ |
76 | @Override |
77 | public Serializable serialize( |
78 | PropertyCollection<?, ?> collection, |
79 | MasterWebSerializer masterSerializer) |
80 | { |
81 | Serializable serializedObject = createSerializedObject(collection); |
82 | |
83 | fillSerializedObject(serializedObject, collection, masterSerializer); |
84 | |
85 | return serializedObject; |
86 | } |
87 | |
88 | /** |
89 | * (non-JSDoc) |
90 | * |
91 | * @see org.deduced.viewer.web.serializer.WebSerializer#fillSerializedObject(java.io.Serializable, |
92 | * org.deduced.PropertyCollection, |
93 | * org.deduced.viewer.web.serializer.MasterWebSerializer) |
94 | */ |
95 | @Override |
96 | public void fillSerializedObject( |
97 | Serializable serializedObject, PropertyCollection<?, ?> collection, |
98 | MasterWebSerializer masterSerializer) |
99 | { |
100 | fillModel(collection, (Model) serializedObject); |
101 | } |
102 | |
103 | /** |
104 | * serialize enumeration |
105 | * |
106 | * @param model the enumeration model |
107 | * @return the serialized name of the enumeration |
108 | */ |
109 | public static String serializeEnumeration( |
110 | PropertyCollection<?, ?> model) |
111 | { |
112 | if (model == null) |
113 | { |
114 | return null; |
115 | } |
116 | |
117 | return DeducedUtilities.getCollectionName(model); |
118 | } |
119 | |
120 | } |