1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.juddi.function;
17
18 import java.util.Vector;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.apache.juddi.datastore.DataStore;
23 import org.apache.juddi.datastore.DataStoreFactory;
24 import org.apache.juddi.datatype.CategoryBag;
25 import org.apache.juddi.datatype.KeyedReference;
26 import org.apache.juddi.datatype.RegistryObject;
27 import org.apache.juddi.datatype.publisher.Publisher;
28 import org.apache.juddi.datatype.request.AuthInfo;
29 import org.apache.juddi.datatype.request.SaveTModel;
30 import org.apache.juddi.datatype.response.TModelDetail;
31 import org.apache.juddi.datatype.tmodel.TModel;
32 import org.apache.juddi.error.InvalidKeyPassedException;
33 import org.apache.juddi.error.RegistryException;
34 import org.apache.juddi.error.UnsupportedException;
35 import org.apache.juddi.error.UserMismatchException;
36 import org.apache.juddi.registry.RegistryEngine;
37 import org.apache.juddi.util.Config;
38 import org.apache.juddi.uuidgen.UUIDGen;
39 import org.apache.juddi.uuidgen.UUIDGenFactory;
40
41 /***
42 * @author Steve Viens (sviens@apache.org)
43 */
44 public class SaveTModelFunction extends AbstractFunction
45 {
46
47 private static Log log = LogFactory.getLog(SaveTModelFunction.class);
48
49 /***
50 *
51 */
52 public SaveTModelFunction(RegistryEngine registry)
53 {
54 super(registry);
55 }
56
57 /***
58 *
59 */
60 public RegistryObject execute(RegistryObject regObject)
61 throws RegistryException
62 {
63 SaveTModel request = (SaveTModel)regObject;
64 String generic = request.getGeneric();
65 AuthInfo authInfo = request.getAuthInfo();
66 Vector tModelVector = request.getTModelVector();
67 Vector uploadRegVector = request.getUploadRegisterVector();
68 UUIDGen uuidgen = UUIDGenFactory.getUUIDGen();
69
70
71 if ((uploadRegVector != null) && (uploadRegVector.size() > 0))
72 throw new UnsupportedException("Saving TModels via " +
73 "UploadRegistry is not supported.");
74
75
76 DataStore dataStore = DataStoreFactory.getDataStore();
77
78 try
79 {
80 dataStore.beginTrans();
81
82
83 Publisher publisher = getPublisher(authInfo,dataStore);
84 String publisherID = publisher.getPublisherID();
85 String authorizedName = publisher.getName();
86
87
88 for (int i=0; i<tModelVector.size(); i++)
89 {
90
91 TModel tModel = (TModel)tModelVector.elementAt(i);
92 String tModelKey = tModel.getTModelKey();
93
94
95 if (((tModelKey != null) && (tModelKey.length() > 0)) && (!dataStore.isValidTModelKey(tModelKey)))
96 throw new InvalidKeyPassedException("save_tModel: "+
97 "tModelKey="+tModelKey);
98
99
100 if (((tModelKey != null) && (tModelKey.length() > 0)) && !dataStore.isTModelPublisher(tModelKey,publisherID))
101 throw new UserMismatchException("save_tModel: "+
102 "userID="+publisherID+", "+
103 "tModelKey="+tModelKey);
104
105
106
107
108
109
110
111
112
113 CategoryBag categoryBag = tModel.getCategoryBag();
114 if (categoryBag != null)
115 {
116 Vector keyedRefVector = categoryBag.getKeyedReferenceVector();
117 if (keyedRefVector != null)
118 {
119 int vectorSize = keyedRefVector.size();
120 if (vectorSize > 0)
121 {
122 for (int j=0; j<vectorSize; j++)
123 {
124 KeyedReference keyedRef = (KeyedReference)keyedRefVector.elementAt(j);
125 String key = keyedRef.getTModelKey();
126
127
128
129
130
131 if ((key == null) || (key.trim().length() == 0))
132 keyedRef.setTModelKey(TModel.GENERAL_KEYWORDS_TMODEL_KEY);
133 }
134 }
135 }
136 }
137 }
138
139 for (int i=0; i<tModelVector.size(); i++)
140 {
141
142 TModel tModel = (TModel)tModelVector.elementAt(i);
143 String tModelKey = tModel.getTModelKey();
144
145
146
147
148 if ((tModelKey != null) && (tModelKey.length() > 0))
149 dataStore.deleteTModel(tModelKey);
150 else
151 tModel.setTModelKey("uuid:"+uuidgen.uuidgen());
152
153
154
155 tModel.setAuthorizedName(authorizedName);
156 tModel.setOperator(Config.getOperator());
157 dataStore.saveTModel(tModel,publisherID);
158 }
159
160 dataStore.commit();
161
162 TModelDetail detail = new TModelDetail();
163 detail.setGeneric(generic);
164 detail.setOperator(Config.getOperator());
165 detail.setTruncated(false);
166 detail.setTModelVector(tModelVector);
167 return detail;
168 }
169 catch(UnsupportedException suppex)
170 {
171 try { dataStore.rollback(); } catch(Exception e) { }
172 log.info(suppex);
173 throw (RegistryException)suppex;
174 }
175 catch(InvalidKeyPassedException ikpex)
176 {
177 try { dataStore.rollback(); } catch(Exception e) { }
178 log.info(ikpex);
179 throw (RegistryException)ikpex;
180 }
181 catch(UserMismatchException umex)
182 {
183 try { dataStore.rollback(); } catch(Exception e) { }
184 log.info(umex);
185 throw (RegistryException)umex;
186 }
187 catch(RegistryException regex)
188 {
189 try { dataStore.rollback(); } catch(Exception e) { }
190 log.error(regex);
191 throw (RegistryException)regex;
192 }
193 catch(Exception ex)
194 {
195 try { dataStore.rollback(); } catch(Exception e) { }
196 log.error(ex);
197 throw new RegistryException(ex);
198 }
199 finally
200 {
201 if (dataStore != null)
202 dataStore.release();
203 }
204 }
205
206
207 /****************************************************************************/
208 /****************************** TEST DRIVER *********************************/
209 /****************************************************************************/
210
211
212 public static void main(String[] args)
213 {
214
215 RegistryEngine reg = new RegistryEngine();
216 reg.init();
217
218 try
219 {
220 }
221 catch (Exception ex)
222 {
223
224 ex.printStackTrace();
225 }
226 finally
227 {
228
229 reg.dispose();
230 }
231 }
232 }
233
234