This project has retired. For details please refer to its Attic page.
UDDIPublicationImpl xref
View Javadoc
1   /*
2    * Copyright 2001-2008 The Apache Software Foundation.
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   */
17  package org.apache.juddi.api.impl;
18  
19  import java.io.StringWriter;
20  import java.io.UnsupportedEncodingException;
21  import java.util.ArrayList;
22  import java.util.Date;
23  import java.util.GregorianCalendar;
24  import java.util.List;
25  import javax.jws.WebService;
26  import javax.persistence.EntityManager;
27  import javax.persistence.EntityTransaction;
28  import javax.persistence.Query;
29  import javax.xml.bind.JAXB;
30  import javax.xml.datatype.DatatypeConfigurationException;
31  import javax.xml.datatype.DatatypeFactory;
32  import javax.xml.ws.Holder;
33  import org.apache.commons.configuration.ConfigurationException;
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  import org.apache.juddi.api.util.PublicationQuery;
37  import org.apache.juddi.api.util.QueryStatus;
38  import org.apache.juddi.config.AppConfig;
39  import org.apache.juddi.config.PersistenceManager;
40  import org.apache.juddi.config.Property;
41  import org.apache.juddi.mapping.MappingApiToModel;
42  import org.apache.juddi.mapping.MappingModelToApi;
43  import org.apache.juddi.model.BindingTemplate;
44  import org.apache.juddi.model.BusinessEntity;
45  import org.apache.juddi.model.BusinessService;
46  import org.apache.juddi.model.ChangeRecord;
47  import org.apache.juddi.model.Signature;
48  import org.apache.juddi.model.Tmodel;
49  import org.apache.juddi.model.UddiEntityPublisher;
50  import org.apache.juddi.query.FetchBusinessEntitiesQuery;
51  import org.apache.juddi.query.FetchTModelsQuery;
52  import org.apache.juddi.query.FindBusinessByPublisherQuery;
53  import org.apache.juddi.query.FindPublisherAssertionByBusinessQuery;
54  import org.apache.juddi.query.FindTModelByPublisherQuery;
55  import org.apache.juddi.query.TModelQuery;
56  import org.apache.juddi.query.util.DynamicQuery;
57  import org.apache.juddi.query.util.FindQualifiers;
58  import org.apache.juddi.replication.ReplicationNotifier;
59  import org.apache.juddi.v3.error.ErrorMessage;
60  import org.apache.juddi.v3.error.FatalErrorException;
61  import org.apache.juddi.v3.error.InvalidValueException;
62  import org.apache.juddi.validation.ValidatePublish;
63  import org.uddi.api_v3.AddPublisherAssertions;
64  import org.uddi.api_v3.AssertionStatusItem;
65  import org.uddi.api_v3.BindingDetail;
66  import org.uddi.api_v3.BusinessDetail;
67  import org.uddi.api_v3.CompletionStatus;
68  import org.uddi.api_v3.DeleteBinding;
69  import org.uddi.api_v3.DeleteBusiness;
70  import org.uddi.api_v3.DeletePublisherAssertions;
71  import org.uddi.api_v3.DeleteService;
72  import org.uddi.api_v3.DeleteTModel;
73  import org.uddi.api_v3.GetRegisteredInfo;
74  import org.uddi.api_v3.InfoSelection;
75  import org.uddi.api_v3.ListDescription;
76  import org.uddi.api_v3.OperationalInfo;
77  import org.uddi.api_v3.PublisherAssertion;
78  import org.uddi.api_v3.RegisteredInfo;
79  import org.uddi.api_v3.SaveBinding;
80  import org.uddi.api_v3.SaveBusiness;
81  import org.uddi.api_v3.SaveService;
82  import org.uddi.api_v3.SaveTModel;
83  import org.uddi.api_v3.ServiceDetail;
84  import org.uddi.api_v3.TModel;
85  import org.uddi.api_v3.TModelDetail;
86  import org.uddi.repl_v3.ChangeRecordDelete;
87  import org.uddi.repl_v3.ChangeRecordDeleteAssertion;
88  import org.uddi.repl_v3.ChangeRecordHide;
89  import org.uddi.repl_v3.ChangeRecordIDType;
90  import org.uddi.repl_v3.ChangeRecordNewData;
91  import org.uddi.repl_v3.ChangeRecordNewDataConditional;
92  import org.uddi.repl_v3.ChangeRecordPublisherAssertion;
93  import org.uddi.v3_service.DispositionReportFaultMessage;
94  import org.uddi.v3_service.UDDIPublicationPortType;
95  
96  /**
97   * This class implements the UDDI Publication Service
98   *
99   * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a> (and many others)
100  * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a> added support for
101  * replication and several bug fixes
102  */
103 @WebService(serviceName = "UDDIPublicationService",
104         endpointInterface = "org.uddi.v3_service.UDDIPublicationPortType",
105         targetNamespace = "urn:uddi-org:api_v3_portType")
106 public class UDDIPublicationImpl extends AuthenticatedService implements UDDIPublicationPortType {
107 
108         private static Log log = LogFactory.getLog(UDDIInquiryImpl.class);
109         private UDDIServiceCounter serviceCounter;
110 
111      
112         public UDDIPublicationImpl() {
113                 super();
114                 serviceCounter = ServiceCounterLifecycleResource.getServiceCounter(UDDIPublicationImpl.class);
115                 
116         }
117 
118         @Override
119         public void addPublisherAssertions(AddPublisherAssertions body)
120                 throws DispositionReportFaultMessage {
121                 long startTime = System.currentTimeMillis();
122 
123                 EntityManager em = PersistenceManager.getEntityManager();
124                 EntityTransaction tx = em.getTransaction();
125                 try {
126                         tx.begin();
127 
128                         UddiEntityPublisher publisher = this.getEntityPublisher(em, body.getAuthInfo());
129 
130                         new ValidatePublish(publisher).validateAddPublisherAssertions(em, body);
131 
132                         List<org.uddi.api_v3.PublisherAssertion> apiPubAssertionList = body.getPublisherAssertion();
133                         List<ChangeRecord> changes = new ArrayList<ChangeRecord>();
134                         for (org.uddi.api_v3.PublisherAssertion apiPubAssertion : apiPubAssertionList) {
135 
136                                 org.apache.juddi.model.PublisherAssertion modelPubAssertion = new org.apache.juddi.model.PublisherAssertion();
137 
138                                 MappingApiToModel.mapPublisherAssertion(apiPubAssertion, modelPubAssertion);
139 
140                                 org.apache.juddi.model.PublisherAssertion existingPubAssertion = em.find(modelPubAssertion.getClass(), modelPubAssertion.getId());
141                                 boolean persistNewAssertion = true;
142                                 if (existingPubAssertion != null) {
143                                         if (modelPubAssertion.getTmodelKey().equalsIgnoreCase(existingPubAssertion.getTmodelKey())
144                                                 && modelPubAssertion.getKeyName().equalsIgnoreCase(existingPubAssertion.getKeyName())
145                                                 && modelPubAssertion.getKeyValue().equalsIgnoreCase(existingPubAssertion.getKeyValue())) {
146                                                 // This pub assertion is already been "asserted".  Simply need to set the "check" value on the existing (and persistent) assertion
147                                                 if (publisher.isOwner(existingPubAssertion.getBusinessEntityByFromKey())) {
148                                                         existingPubAssertion.setFromCheck("true");
149                                                 }
150                                                 if (publisher.isOwner(existingPubAssertion.getBusinessEntityByToKey())) {
151                                                         existingPubAssertion.setToCheck("true");
152                                                 }
153                                                 //it's also possible that the signatures have changed
154                                                 removeExistingPublisherAssertionSignatures(existingPubAssertion.getBusinessEntityByFromKey().getEntityKey(), existingPubAssertion.getBusinessEntityByToKey().getEntityKey(), em);
155                                                 savePushliserAssertionSignatures(existingPubAssertion.getBusinessEntityByFromKey().getEntityKey(), existingPubAssertion.getBusinessEntityByToKey().getEntityKey(), modelPubAssertion.getSignatures(), em);
156 
157                                                 em.merge(existingPubAssertion);
158                                                 persistNewAssertion = false;
159                                                 changes.add(getChangeRecord_deletePublisherAssertion(apiPubAssertion, getNode(), existingPubAssertion.getToCheck().equalsIgnoreCase("false"), existingPubAssertion.getFromCheck().equalsIgnoreCase("false"), System.currentTimeMillis()));
160                                         } else {
161                                                 // Otherwise, it is a new relationship between these entities.  Remove the old one so the new one can be added.
162                                                 // TODO: the model only seems to allow one assertion per two business (primary key is fromKey and toKey). Spec seems to imply as 
163                                                 // many relationships as desired (the differentiator would be the keyedRef values).
164                                                 removeExistingPublisherAssertionSignatures(existingPubAssertion.getBusinessEntityByFromKey().getEntityKey(), existingPubAssertion.getBusinessEntityByToKey().getEntityKey(), em);
165                                                 em.remove(existingPubAssertion);
166                                                 changes.add(getChangeRecord_deletePublisherAssertion(apiPubAssertion, getNode(), true, true, System.currentTimeMillis()));
167                                         }
168                                 }
169 
170                                 if (persistNewAssertion) {
171                                         org.apache.juddi.model.BusinessEntity beFrom = em.find(org.apache.juddi.model.BusinessEntity.class, modelPubAssertion.getId().getFromKey());
172                                         org.apache.juddi.model.BusinessEntity beTo = em.find(org.apache.juddi.model.BusinessEntity.class, modelPubAssertion.getId().getToKey());
173                                         modelPubAssertion.setBusinessEntityByFromKey(beFrom);
174                                         modelPubAssertion.setBusinessEntityByToKey(beTo);
175 
176                                         modelPubAssertion.setFromCheck("false");
177                                         modelPubAssertion.setToCheck("false");
178 
179                                         if (publisher.isOwner(modelPubAssertion.getBusinessEntityByFromKey())) {
180                                                 modelPubAssertion.setFromCheck("true");
181                                         }
182                                         if (publisher.isOwner(modelPubAssertion.getBusinessEntityByToKey())) {
183                                                 modelPubAssertion.setToCheck("true");
184                                         }
185                                         modelPubAssertion.setModified(new Date());
186                                          savePushliserAssertionSignatures(modelPubAssertion.getBusinessEntityByFromKey().getEntityKey(), modelPubAssertion.getBusinessEntityByToKey().getEntityKey(), modelPubAssertion.getSignatures(), em);
187 
188                                         em.persist(modelPubAssertion);
189 
190                                         changes.add(getChangeRecord_NewAssertion(apiPubAssertion, modelPubAssertion, getNode()));
191 
192                                 }
193 
194                         }
195 
196                         tx.commit();
197                         for (int i = 0; i < changes.size(); i++) {
198                                 ReplicationNotifier.enqueue(changes.get(i));
199                         }
200 
201                         long procTime = System.currentTimeMillis() - startTime;
202                         serviceCounter.update(PublicationQuery.ADD_PUBLISHERASSERTIONS,
203                                 QueryStatus.SUCCESS, procTime);
204                 } catch (DispositionReportFaultMessage drfm) {
205                         long procTime = System.currentTimeMillis() - startTime;
206                         serviceCounter.update(PublicationQuery.ADD_PUBLISHERASSERTIONS, QueryStatus.FAILED, procTime);
207                         throw drfm;
208                 } finally {
209                         if (tx.isActive()) {
210                                 tx.rollback();
211                         }
212                         em.close();
213                 }
214         }
215 
216         public void deleteBinding(DeleteBinding body)
217                 throws DispositionReportFaultMessage {
218                 long startTime = System.currentTimeMillis();
219 
220                 EntityManager em = PersistenceManager.getEntityManager();
221                 EntityTransaction tx = em.getTransaction();
222                 try {
223                         tx.begin();
224 
225                         UddiEntityPublisher publisher = this.getEntityPublisher(em, body.getAuthInfo());
226 
227                         new ValidatePublish(publisher).validateDeleteBinding(em, body);
228 
229                         List<String> entityKeyList = body.getBindingKey();
230                         List<ChangeRecord> changes = new ArrayList<ChangeRecord>();
231                         for (String entityKey : entityKeyList) {
232                                 deleteBinding(entityKey, em);
233                                 changes.add(getChangeRecord_deleteBinding(entityKey, getNode()));
234                         }
235                         tx.commit();
236                         for (int i = 0; i < changes.size(); i++) {
237                                 ReplicationNotifier.enqueue(changes.get(i));
238                         }
239 
240                         long procTime = System.currentTimeMillis() - startTime;
241                         serviceCounter.update(PublicationQuery.DELETE_BINDING,
242                                 QueryStatus.SUCCESS, procTime);
243                 } catch (DispositionReportFaultMessage drfm) {
244                         long procTime = System.currentTimeMillis() - startTime;
245                         serviceCounter.update(PublicationQuery.DELETE_BINDING, QueryStatus.FAILED, procTime);
246                         throw drfm;
247                 } finally {
248                         if (tx.isActive()) {
249                                 tx.rollback();
250                         }
251                         em.close();
252                 }
253         }
254 
255         /**
256          * deletes the referenced object, assuming authorization rules are
257          * already processed and there is already an open transaction
258          *
259          * @param entityKey
260          * @param em
261          * @throws DispositionReportFaultMessage
262          */
263         protected void deleteBinding(String entityKey, EntityManager em) throws DispositionReportFaultMessage {
264 
265                 Object obj = em.find(org.apache.juddi.model.BindingTemplate.class, entityKey);
266 
267                 ((org.apache.juddi.model.BindingTemplate) obj).getBusinessService().setModifiedIncludingChildren(new Date());
268                 // JUDDI-421:  now the businessEntity parent will have it's modifiedIncludingChildren set
269                 ((org.apache.juddi.model.BindingTemplate) obj).getBusinessService().getBusinessEntity().setModifiedIncludingChildren(new Date());
270 
271                 em.remove(obj);
272 
273         }
274 
275         public void deleteBusiness(DeleteBusiness body)
276                 throws DispositionReportFaultMessage {
277                 long startTime = System.currentTimeMillis();
278 
279                 EntityManager em = PersistenceManager.getEntityManager();
280                 EntityTransaction tx = em.getTransaction();
281                 try {
282                         tx.begin();
283 
284                         UddiEntityPublisher publisher = this.getEntityPublisher(em, body.getAuthInfo());
285 
286                         new ValidatePublish(publisher).validateDeleteBusiness(em, body);
287 
288                         List<String> entityKeyList = body.getBusinessKey();
289                         List<ChangeRecord> changes = new ArrayList<ChangeRecord>();
290                         for (String entityKey : entityKeyList) {
291                                 deleteBusiness(entityKey, em);
292                                 changes.add(getChangeRecord_deleteBusiness(entityKey, getNode()));
293                         }
294 
295                         tx.commit();
296                         for (int i = 0; i < changes.size(); i++) {
297                                 ReplicationNotifier.enqueue(changes.get(i));
298                         }
299                         long procTime = System.currentTimeMillis() - startTime;
300                         serviceCounter.update(PublicationQuery.DELETE_BUSINESS, QueryStatus.SUCCESS, procTime);
301                 } catch (DispositionReportFaultMessage drfm) {
302                         long procTime = System.currentTimeMillis() - startTime;
303                         serviceCounter.update(PublicationQuery.DELETE_BUSINESS, QueryStatus.FAILED, procTime);
304                         throw drfm;
305                 } finally {
306                         if (tx.isActive()) {
307                                 tx.rollback();
308                         }
309                         em.close();
310                 }
311         }
312 
313         /**
314          * deletes the referenced object, assuming authorization rules are
315          * already processed and there is already an open transaction
316          *
317          * @param entityKey
318          * @param em
319          * @throws DispositionReportFaultMessage
320          */
321         protected void deleteBusiness(String key, EntityManager em) throws DispositionReportFaultMessage {
322                 Object obj = em.find(org.apache.juddi.model.BusinessEntity.class, key);
323                 em.remove(obj);
324         }
325 
326         public void deletePublisherAssertions(DeletePublisherAssertions body)
327                 throws DispositionReportFaultMessage {
328                 long startTime = System.currentTimeMillis();
329 
330                 EntityManager em = PersistenceManager.getEntityManager();
331                 EntityTransaction tx = em.getTransaction();
332                 try {
333                         tx.begin();
334 
335                         UddiEntityPublisher publisher = this.getEntityPublisher(em, body.getAuthInfo());
336 
337                         new ValidatePublish(publisher).validateDeletePublisherAssertions(em, body);
338 
339                         List<org.uddi.api_v3.PublisherAssertion> entityList = body.getPublisherAssertion();
340                         List<ChangeRecord> changes = new ArrayList<ChangeRecord>();
341                         for (org.uddi.api_v3.PublisherAssertion entity : entityList) {
342                                 org.apache.juddi.model.PublisherAssertion modelPubAssertion = new org.apache.juddi.model.PublisherAssertion();
343 
344                                 MappingApiToModel.mapPublisherAssertion(entity, modelPubAssertion);
345 
346                                 org.apache.juddi.model.PublisherAssertion existingPubAssertion = em.find(org.apache.juddi.model.PublisherAssertion.class,
347                                         modelPubAssertion.getId());
348                                 if (existingPubAssertion == null) {
349                                         throw new InvalidValueException(new ErrorMessage("E_assertionNotFound"));
350                                 }
351 
352                                 boolean fromkey = publisher.isOwner(em.find(BusinessEntity.class, entity.getFromKey()));
353                                 boolean tokey = publisher.isOwner(em.find(BusinessEntity.class, entity.getToKey()));
354                                 if (fromkey) {
355                                         existingPubAssertion.setFromCheck("false");
356                                 }
357                                 if (tokey) {
358                                         existingPubAssertion.setToCheck("false");
359                                 }
360                                 if ("false".equalsIgnoreCase(existingPubAssertion.getToCheck())
361                                         && "false".equalsIgnoreCase(existingPubAssertion.getFromCheck())) {
362                                         logger.info("Publisher assertion updated database via replication");
363                                         removeExistingPublisherAssertionSignatures(existingPubAssertion.getBusinessEntityByFromKey().getEntityKey(), existingPubAssertion.getBusinessEntityByToKey().getEntityKey(), em);
364                                         em.remove(existingPubAssertion);
365                                 } else {
366                                         existingPubAssertion.setModified(new Date());
367                                         logger.info("Publisher assertion updated database via replication");
368                                         removeExistingPublisherAssertionSignatures(existingPubAssertion.getBusinessEntityByFromKey().getEntityKey(), existingPubAssertion.getBusinessEntityByToKey().getEntityKey(), em);
369                                         savePushliserAssertionSignatures(existingPubAssertion.getBusinessEntityByFromKey().getEntityKey(),
370                                                 existingPubAssertion.getBusinessEntityByToKey().getEntityKey(), modelPubAssertion.getSignatures(), em);
371                                         em.persist(existingPubAssertion);
372                                 }
373 
374                                 changes.add(getChangeRecord_deletePublisherAssertion(entity, getNode(), tokey, fromkey, existingPubAssertion.getModified().getTime()));
375                         }
376 
377                         tx.commit();
378                         for (int i = 0; i < changes.size(); i++) {
379                                 ReplicationNotifier.enqueue(changes.get(i));
380                         }
381                         long procTime = System.currentTimeMillis() - startTime;
382                         serviceCounter.update(PublicationQuery.DELETE_PUBLISHERASSERTIONS,
383                                 QueryStatus.SUCCESS, procTime);
384                 } catch (DispositionReportFaultMessage drfm) {
385                         long procTime = System.currentTimeMillis() - startTime;
386                         serviceCounter.update(PublicationQuery.DELETE_PUBLISHERASSERTIONS, QueryStatus.FAILED, procTime);
387                         throw drfm;
388                 } finally {
389                         if (tx.isActive()) {
390                                 tx.rollback();
391                         }
392                         em.close();
393                 }
394         }
395 
396         /**
397          * deletes the referenced object, assuming authorization rules are
398          * already processed and there is already an open transaction. this is
399          * primarily used to support replication calls, i.e. another node just
400          * changed a PA record and let us know
401          *
402          * @param entityKey
403          * @param em
404          * @throws DispositionReportFaultMessage
405          */
406         protected void deletePublisherAssertion(org.uddi.repl_v3.ChangeRecordDeleteAssertion entity, EntityManager em) throws DispositionReportFaultMessage {
407 
408                 org.apache.juddi.model.PublisherAssertion modelPubAssertion = new org.apache.juddi.model.PublisherAssertion();
409 
410                 MappingApiToModel.mapPublisherAssertion(entity.getPublisherAssertion(), modelPubAssertion);
411 
412                 org.apache.juddi.model.PublisherAssertion existingPubAssertion = em.find(org.apache.juddi.model.PublisherAssertion.class,
413                         modelPubAssertion.getId());
414 
415                 if (existingPubAssertion == null) {
416                         throw new FatalErrorException(new ErrorMessage("E_assertionNotFound"));
417                 }
418                 boolean fromkey = entity.isFromBusinessCheck();// publisher.isOwner(em.find(BusinessEntity.class, entity.getFromKey()));
419                 boolean tokey = entity.isToBusinessCheck();//  publisher.isOwner(em.find(BusinessEntity.class, entity.getToKey()));
420                 if (fromkey) {
421                         existingPubAssertion.setFromCheck("false");
422                 }
423                 if (tokey) {
424                         existingPubAssertion.setToCheck("false");
425                 }
426                 if ("false".equalsIgnoreCase(existingPubAssertion.getToCheck())
427                         && "false".equalsIgnoreCase(existingPubAssertion.getFromCheck())) {
428                         logger.info("Deletion of publisher assertion from database via replication");
429                         removeExistingPublisherAssertionSignatures(existingPubAssertion.getBusinessEntityByFromKey().getEntityKey(), existingPubAssertion.getBusinessEntityByToKey().getEntityKey(), em);
430                         em.remove(existingPubAssertion);
431                 } else {
432                         existingPubAssertion.setModified(new Date());
433                         logger.info("Publisher assertion updated database via replication");
434                         removeExistingPublisherAssertionSignatures(existingPubAssertion.getBusinessEntityByFromKey().getEntityKey(), existingPubAssertion.getBusinessEntityByToKey().getEntityKey(), em);
435                         savePushliserAssertionSignatures(existingPubAssertion.getBusinessEntityByFromKey().getEntityKey(),
436                                 existingPubAssertion.getBusinessEntityByToKey().getEntityKey(), modelPubAssertion.getSignatures(), em);
437                         em.persist(existingPubAssertion);
438                 }
439 
440         }
441 
442         public void deleteService(DeleteService body)
443                 throws DispositionReportFaultMessage {
444                 long startTime = System.currentTimeMillis();
445 
446                 EntityManager em = PersistenceManager.getEntityManager();
447                 EntityTransaction tx = em.getTransaction();
448                 try {
449                         tx.begin();
450 
451                         UddiEntityPublisher publisher = this.getEntityPublisher(em, body.getAuthInfo());
452 
453                         new ValidatePublish(publisher).validateDeleteService(em, body);
454 
455                         List<String> entityKeyList = body.getServiceKey();
456                         List<ChangeRecord> changes = new ArrayList<ChangeRecord>();
457                         for (String entityKey : entityKeyList) {
458                                 deleteService(entityKey, em);
459                                 changes.add(getChangeRecord_deleteService(entityKey, getNode()));
460                         }
461 
462                         tx.commit();
463                         for (int i = 0; i < changes.size(); i++) {
464                                 ReplicationNotifier.enqueue(changes.get(i));
465                         }
466                         long procTime = System.currentTimeMillis() - startTime;
467                         serviceCounter.update(PublicationQuery.DELETE_SERVICE,
468                                 QueryStatus.SUCCESS, procTime);
469                 } catch (DispositionReportFaultMessage drfm) {
470                         long procTime = System.currentTimeMillis() - startTime;
471                         serviceCounter.update(PublicationQuery.DELETE_SERVICE, QueryStatus.FAILED, procTime);
472                         throw drfm;
473                 } finally {
474                         if (tx.isActive()) {
475                                 tx.rollback();
476                         }
477                         em.close();
478                 }
479         }
480 
481         /**
482          * deletes the referenced object, assuming authorization rules are
483          * already processed and there is already an open transaction
484          *
485          * @param entityKey
486          * @param em
487          * @throws DispositionReportFaultMessage
488          */
489         protected void deleteService(String key, EntityManager em) throws DispositionReportFaultMessage {
490                 Object obj = em.find(org.apache.juddi.model.BusinessService.class, key);
491                 //((org.apache.juddi.model.BusinessService) obj).getBusinessEntity().setModifiedIncludingChildren(new Date());
492                 if (obj != null) {
493                         em.remove(obj);
494                 } else {
495                         logger.warn("Unable to remove service with the key '" + key + "', it doesn't exist in the database");
496                 }
497         }
498 
499         /**
500          * {@inheritDoc }
501          */
502         @Override
503         public void deleteTModel(DeleteTModel body)
504                 throws DispositionReportFaultMessage {
505                 long startTime = System.currentTimeMillis();
506 
507                 EntityManager em = PersistenceManager.getEntityManager();
508                 EntityTransaction tx = em.getTransaction();
509                 try {
510                         tx.begin();
511 
512                         UddiEntityPublisher publisher = this.getEntityPublisher(em, body.getAuthInfo());
513 
514                         new ValidatePublish(publisher).validateDeleteTModel(em, body);
515 
516                         // tModels are only lazily deleted!
517                         List<String> entityKeyList = body.getTModelKey();
518                         List<ChangeRecord> changes = new ArrayList<ChangeRecord>();
519                         for (String entityKey : entityKeyList) {
520                                 deleteTModel(entityKey, em);
521                                 changes.add(getChangeRecord_deleteTModelHide(entityKey, getNode()));
522                         }
523 
524                         tx.commit();
525 
526                         for (int i = 0; i < changes.size(); i++) {
527                                 ReplicationNotifier.enqueue(changes.get(i));
528                         }
529                         long procTime = System.currentTimeMillis() - startTime;
530                         serviceCounter.update(PublicationQuery.DELETE_TMODEL, QueryStatus.SUCCESS, procTime);
531                 } catch (DispositionReportFaultMessage drfm) {
532                         long procTime = System.currentTimeMillis() - startTime;
533                         serviceCounter.update(PublicationQuery.DELETE_TMODEL, QueryStatus.FAILED, procTime);
534                         throw drfm;
535                 } finally {
536                         if (tx.isActive()) {
537                                 tx.rollback();
538                         }
539                         em.close();
540                 }
541         }
542 
543         /**
544          * deletes the referenced object, assuming authorization rules are
545          * already processed and there is already an open transaction
546          *
547          * @param entityKey
548          * @param em
549          * @throws DispositionReportFaultMessage
550          */
551         protected void deleteTModel(String key, EntityManager em) {
552                 Object obj = em.find(org.apache.juddi.model.Tmodel.class, key);
553                 ((org.apache.juddi.model.Tmodel) obj).setDeleted(true);
554         }
555 
556         /**
557          * {@inheritDoc }
558          */
559         @Override
560         public List<AssertionStatusItem> getAssertionStatusReport(String authInfo,
561                 CompletionStatus completionStatus)
562                 throws DispositionReportFaultMessage {
563                 long startTime = System.currentTimeMillis();
564 
565                 EntityManager em = PersistenceManager.getEntityManager();
566                 EntityTransaction tx = em.getTransaction();
567                 try {
568                         tx.begin();
569 
570                         UddiEntityPublisher publisher = this.getEntityPublisher(em, authInfo);
571 
572                         List<org.uddi.api_v3.AssertionStatusItem> result = PublicationHelper.getAssertionStatusItemList(publisher, completionStatus, em);
573 
574                         tx.commit();
575                         long procTime = System.currentTimeMillis() - startTime;
576                         serviceCounter.update(PublicationQuery.GET_ASSERTIONSTATUSREPORT,
577                                 QueryStatus.SUCCESS, procTime);
578 
579                         return result;
580                 } catch (DispositionReportFaultMessage drfm) {
581                         long procTime = System.currentTimeMillis() - startTime;
582                         serviceCounter.update(PublicationQuery.GET_ASSERTIONSTATUSREPORT, QueryStatus.FAILED, procTime);
583                         throw drfm;
584                 } finally {
585                         if (tx.isActive()) {
586                                 tx.rollback();
587                         }
588                         em.close();
589                 }
590         }
591 
592         public List<PublisherAssertion> getPublisherAssertions(String authInfo)
593                 throws DispositionReportFaultMessage {
594                 long startTime = System.currentTimeMillis();
595 
596                 EntityManager em = PersistenceManager.getEntityManager();
597                 EntityTransaction tx = em.getTransaction();
598                 try {
599                         tx.begin();
600 
601                         UddiEntityPublisher publisher = this.getEntityPublisher(em, authInfo);
602 
603                         List<org.uddi.api_v3.PublisherAssertion> result = new ArrayList<org.uddi.api_v3.PublisherAssertion>(0);
604 
605                         List<?> businessKeysFound = null;
606                         businessKeysFound = FindBusinessByPublisherQuery.select(em, null, publisher, businessKeysFound);
607 
608                         List<org.apache.juddi.model.PublisherAssertion> pubAssertionList = FindPublisherAssertionByBusinessQuery.select(em, businessKeysFound, null);
609                         if (pubAssertionList != null) {
610                                 for (org.apache.juddi.model.PublisherAssertion modelPubAssertion : pubAssertionList) {
611                                         org.uddi.api_v3.PublisherAssertion apiPubAssertion = new org.uddi.api_v3.PublisherAssertion();
612 
613                                         MappingModelToApi.mapPublisherAssertion(modelPubAssertion, apiPubAssertion);
614 
615                                         result.add(apiPubAssertion);
616                                 }
617                         }
618 
619                         tx.commit();
620                         long procTime = System.currentTimeMillis() - startTime;
621                         serviceCounter.update(PublicationQuery.GET_PUBLISHERASSERTIONS,
622                                 QueryStatus.SUCCESS, procTime);
623                         return result;
624                 } catch (DispositionReportFaultMessage drfm) {
625                         long procTime = System.currentTimeMillis() - startTime;
626                         serviceCounter.update(PublicationQuery.GET_PUBLISHERASSERTIONS,
627                                 QueryStatus.FAILED, procTime);
628                         throw drfm;
629                 } finally {
630                         if (tx.isActive()) {
631                                 tx.rollback();
632                         }
633                         em.close();
634                 }
635         }
636 
637         /**
638          * {@inheritdoc}
639          *
640          */
641         public RegisteredInfo getRegisteredInfo(GetRegisteredInfo body)
642                 throws DispositionReportFaultMessage {
643                 long startTime = System.currentTimeMillis();
644 
645                 EntityManager em = PersistenceManager.getEntityManager();
646                 EntityTransaction tx = em.getTransaction();
647                 try {
648                         tx.begin();
649 
650                         UddiEntityPublisher publisher = this.getEntityPublisher(em, body.getAuthInfo());
651 
652                         new ValidatePublish(publisher).validateRegisteredInfo(body);
653 
654                         List<?> businessKeysFound = null;
655                         businessKeysFound = FindBusinessByPublisherQuery.select(em, null, publisher, businessKeysFound);
656 
657                         List<?> tmodelKeysFound = null;
658 
659                         if (body.getInfoSelection().equals(InfoSelection.HIDDEN)) {
660                                 tmodelKeysFound = FindTModelByPublisherQuery.select(em, null, publisher, tmodelKeysFound, new DynamicQuery.Parameter(TModelQuery.ENTITY_ALIAS + ".deleted", Boolean.TRUE, DynamicQuery.PREDICATE_EQUALS));
661                         } else if (body.getInfoSelection().equals(InfoSelection.VISIBLE)) {
662                                 tmodelKeysFound = FindTModelByPublisherQuery.select(em, null, publisher, tmodelKeysFound, new DynamicQuery.Parameter(TModelQuery.ENTITY_ALIAS + ".deleted", Boolean.FALSE, DynamicQuery.PREDICATE_EQUALS));
663                         } else {
664                                 tmodelKeysFound = FindTModelByPublisherQuery.select(em, null, publisher, tmodelKeysFound);
665                         }
666 
667                         RegisteredInfo result = new RegisteredInfo();
668 
669                         // Sort and retrieve the final results
670                         List<?> queryResults = FetchBusinessEntitiesQuery.select(em, new FindQualifiers(), businessKeysFound, null, null, null);
671                         if (queryResults.size() > 0) {
672                                 result.setBusinessInfos(new org.uddi.api_v3.BusinessInfos());
673 
674                                 for (Object item : queryResults) {
675                                         org.apache.juddi.model.BusinessEntity modelBusinessEntity = (org.apache.juddi.model.BusinessEntity) item;
676                                         org.uddi.api_v3.BusinessInfo apiBusinessInfo = new org.uddi.api_v3.BusinessInfo();
677 
678                                         MappingModelToApi.mapBusinessInfo(modelBusinessEntity, apiBusinessInfo);
679 
680                                         result.getBusinessInfos().getBusinessInfo().add(apiBusinessInfo);
681                                 }
682                         }
683 
684                         // Sort and retrieve the final results
685                         queryResults = FetchTModelsQuery.select(em, new FindQualifiers(), tmodelKeysFound, null, null, null);
686                         if (queryResults.size() > 0) {
687                                 result.setTModelInfos(new org.uddi.api_v3.TModelInfos());
688 
689                                 for (Object item : queryResults) {
690                                         org.apache.juddi.model.Tmodel modelTModel = (org.apache.juddi.model.Tmodel) item;
691                                         org.uddi.api_v3.TModelInfo apiTModelInfo = new org.uddi.api_v3.TModelInfo();
692 
693                                         MappingModelToApi.mapTModelInfo(modelTModel, apiTModelInfo);
694 
695                                         result.getTModelInfos().getTModelInfo().add(apiTModelInfo);
696                                 }
697                         }
698 
699                         tx.commit();
700                         long procTime = System.currentTimeMillis() - startTime;
701                         serviceCounter.update(PublicationQuery.GET_REGISTEREDINFO,
702                                 QueryStatus.SUCCESS, procTime);
703 
704                         return result;
705                 } catch (DispositionReportFaultMessage drfm) {
706                         long procTime = System.currentTimeMillis() - startTime;
707                         serviceCounter.update(PublicationQuery.GET_REGISTEREDINFO,
708                                 QueryStatus.FAILED, procTime);
709                         throw drfm;
710                 } finally {
711                         if (tx.isActive()) {
712                                 tx.rollback();
713                         }
714                         em.close();
715                 }
716         }
717 
718         /**
719          * {@inheritdoc}
720          *
721          */
722         public BindingDetail saveBinding(SaveBinding body)
723                 throws DispositionReportFaultMessage {
724                 long startTime = System.currentTimeMillis();
725 
726                 EntityManager em = PersistenceManager.getEntityManager();
727                 EntityTransaction tx = em.getTransaction();
728                 try {
729                         tx.begin();
730 
731                         UddiEntityPublisher publisher = this.getEntityPublisher(em, body.getAuthInfo());
732                         publisher.populateKeyGeneratorKeys(em);
733                         ValidatePublish validator = new ValidatePublish(publisher);
734                         validator.validateSaveBinding(em, body, null, publisher);
735 
736                         BindingDetail result = new BindingDetail();
737                         result.setListDescription(new ListDescription());
738                         List<org.uddi.api_v3.BindingTemplate> apiBindingTemplateList = body.getBindingTemplate();
739                         List<org.apache.juddi.model.ChangeRecord> changes = new ArrayList<ChangeRecord>();
740 
741                         for (org.uddi.api_v3.BindingTemplate apiBindingTemplate : apiBindingTemplateList) {
742 
743                                 org.apache.juddi.model.BindingTemplate modelBindingTemplate = new org.apache.juddi.model.BindingTemplate();
744 
745                                 org.apache.juddi.model.BusinessService modelBusinessService = new org.apache.juddi.model.BusinessService();
746                                 modelBusinessService.setEntityKey(apiBindingTemplate.getServiceKey());
747 
748                                 MappingApiToModel.mapBindingTemplate(apiBindingTemplate, modelBindingTemplate, modelBusinessService);
749 
750                                 setOperationalInfo(em, modelBindingTemplate, publisher, true);
751 
752                                 em.persist(modelBindingTemplate);
753 
754                                 result.getBindingTemplate().add(apiBindingTemplate);
755                                 result.getListDescription().setActualCount(result.getListDescription().getActualCount() + 1);
756                                 result.getListDescription().setIncludeCount(result.getListDescription().getIncludeCount() + 1);
757                                 validator.validateSaveBindingMax(em, modelBindingTemplate.getBusinessService().getEntityKey());
758                                 changes.add(getChangeRecord(modelBindingTemplate, apiBindingTemplate, getNode()));
759                         }
760 
761                         tx.commit();
762                         for (int i = 0; i < changes.size(); i++) {
763                                 ReplicationNotifier.enqueue(changes.get(i));
764                         }
765                         long procTime = System.currentTimeMillis() - startTime;
766                         serviceCounter.update(PublicationQuery.SAVE_BINDING,
767                                 QueryStatus.SUCCESS, procTime);
768 
769                         return result;
770                 } catch (DispositionReportFaultMessage drfm) {
771                         long procTime = System.currentTimeMillis() - startTime;
772                         serviceCounter.update(PublicationQuery.SAVE_BINDING,
773                                 QueryStatus.FAILED, procTime);
774                         throw drfm;
775                 } finally {
776                         if (tx.isActive()) {
777                                 tx.rollback();
778                         }
779                         em.close();
780                 }
781         }
782 
783         /**
784          * {@inheritdoc}
785          *
786          */
787         public BusinessDetail saveBusiness(SaveBusiness body)
788                 throws DispositionReportFaultMessage {
789                 long startTime = System.currentTimeMillis();
790                 
791                 EntityManager em = PersistenceManager.getEntityManager();
792                 EntityTransaction tx = em.getTransaction();
793                 try {
794                         tx.begin();
795 
796                         UddiEntityPublisher publisher = this.getEntityPublisher(em, body.getAuthInfo());
797                         publisher.populateKeyGeneratorKeys(em);
798                         ValidatePublish validator = new ValidatePublish(publisher);
799                         validator.validateSaveBusiness(em, body, null, publisher);
800 
801                         BusinessDetail result = new BusinessDetail();
802 
803                         List<org.uddi.api_v3.BusinessEntity> apiBusinessEntityList = body.getBusinessEntity();
804                         List<ChangeRecord> changes = new ArrayList<ChangeRecord>();
805 
806                         for (org.uddi.api_v3.BusinessEntity apiBusinessEntity : apiBusinessEntityList) {
807 
808                                 org.apache.juddi.model.BusinessEntity modelBusinessEntity = new org.apache.juddi.model.BusinessEntity();
809 
810                                 MappingApiToModel.mapBusinessEntity(apiBusinessEntity, modelBusinessEntity);
811 
812                                 setOperationalInfo(em, modelBusinessEntity, publisher);
813                                 log.debug("Saving business " + modelBusinessEntity.getEntityKey());
814 
815                                 em.persist(modelBusinessEntity);
816                                 changes.add(getChangeRecord(modelBusinessEntity, apiBusinessEntity, getNode()));
817                                 result.getBusinessEntity().add(apiBusinessEntity);
818                         }
819 
820                         //check how many business this publisher owns.
821                         validator.validateSaveBusinessMax(em);
822 
823                         tx.commit();
824                         for (int i = 0; i < changes.size(); i++) {
825                                 ReplicationNotifier.enqueue(changes.get(i));
826                         }
827                         long procTime = System.currentTimeMillis() - startTime;
828                         serviceCounter.update(PublicationQuery.SAVE_BUSINESS,
829                                 QueryStatus.SUCCESS, procTime);
830 
831                         return result;
832                 } catch (DispositionReportFaultMessage drfm) {
833                         long procTime = System.currentTimeMillis() - startTime;
834                         serviceCounter.update(PublicationQuery.SAVE_BUSINESS,
835                                 QueryStatus.FAILED, procTime);
836                         throw drfm;
837                 } catch (Exception ex) {
838                         StringWriter sw = new StringWriter();
839                         if (body != null) {
840                                 JAXB.marshal(body, sw);
841                         }
842                         log.fatal("unexpected error!" + sw.toString(), ex);
843                         throw new FatalErrorException(new ErrorMessage("E_fatalError", ex.getMessage()));
844                 } finally {
845                         if (tx.isActive()) {
846                                 tx.rollback();
847                         }
848                         em.close();
849                 }
850         }
851 
852         /**
853          * {@inheritdoc}
854          *
855          */
856         public ServiceDetail saveService(SaveService body)
857                 throws DispositionReportFaultMessage {
858                 long startTime = System.currentTimeMillis();
859 
860                 EntityManager em = PersistenceManager.getEntityManager();
861                 EntityTransaction tx = em.getTransaction();
862                 try {
863                         tx.begin();
864 
865                         UddiEntityPublisher publisher = this.getEntityPublisher(em, body.getAuthInfo());
866                         publisher.populateKeyGeneratorKeys(em);
867                         ValidatePublish validator = new ValidatePublish(publisher);
868                         validator.validateSaveService(em, body, null, publisher);
869 
870                         ServiceDetail result = new ServiceDetail();
871 
872                         List<org.uddi.api_v3.BusinessService> apiBusinessServiceList = body.getBusinessService();
873                         List<ChangeRecord> changes = new ArrayList<ChangeRecord>();
874                         for (org.uddi.api_v3.BusinessService apiBusinessService : apiBusinessServiceList) {
875 
876                                 org.apache.juddi.model.BusinessService modelBusinessService = new org.apache.juddi.model.BusinessService();
877                                 org.apache.juddi.model.BusinessEntity modelBusinessEntity = new org.apache.juddi.model.BusinessEntity();
878                                 modelBusinessEntity.setEntityKey(apiBusinessService.getBusinessKey());
879 
880                                 MappingApiToModel.mapBusinessService(apiBusinessService, modelBusinessService, modelBusinessEntity);
881 
882                                 setOperationalInfo(em, modelBusinessService, publisher, false);
883 
884                                 em.persist(modelBusinessService);
885 
886                                 result.getBusinessService().add(apiBusinessService);
887 
888                                 validator.validateSaveServiceMax(em, modelBusinessService.getBusinessEntity().getEntityKey());
889                                 changes.add(getChangeRecord(modelBusinessService, apiBusinessService, getNode()));
890                         }
891 
892                         tx.commit();
893                         for (int i = 0; i < changes.size(); i++) {
894                                 ReplicationNotifier.enqueue(changes.get(i));
895                         }
896                         long procTime = System.currentTimeMillis() - startTime;
897                         serviceCounter.update(PublicationQuery.SAVE_SERVICE,
898                                 QueryStatus.SUCCESS, procTime);
899 
900                         return result;
901                 } catch (DispositionReportFaultMessage drfm) {
902                         long procTime = System.currentTimeMillis() - startTime;
903                         serviceCounter.update(PublicationQuery.SAVE_SERVICE,
904                                 QueryStatus.FAILED, procTime);
905                         throw drfm;
906                 } finally {
907                         if (tx.isActive()) {
908                                 tx.rollback();
909                         }
910                         em.close();
911                 }
912         }
913 
914         /**
915          * {@inheritdoc}
916          *
917          */
918         @Override
919         public TModelDetail saveTModel(SaveTModel body)
920                 throws DispositionReportFaultMessage {
921                 long startTime = System.currentTimeMillis();
922 
923                 EntityManager em = PersistenceManager.getEntityManager();
924                 EntityTransaction tx = em.getTransaction();
925                 try {
926                         tx.begin();
927 
928                         UddiEntityPublisher publisher = this.getEntityPublisher(em, body.getAuthInfo());
929                         publisher.populateKeyGeneratorKeys(em);
930                         new ValidatePublish(publisher).validateSaveTModel(em, body, null, publisher);
931 
932                         TModelDetail result = new TModelDetail();
933 
934                         List<org.uddi.api_v3.TModel> apiTModelList = body.getTModel();
935                         List<ChangeRecord> changes = new ArrayList<ChangeRecord>();
936                         for (org.uddi.api_v3.TModel apiTModel : apiTModelList) {
937 
938                                 // Object obj=em.find( org.apache.juddi.model.Tmodel.class, apiTModel.getTModelKey());
939                                 //just making changes to an existing tModel, no worries
940                                 org.apache.juddi.model.Tmodel modelTModel = new org.apache.juddi.model.Tmodel();
941 
942                                 MappingApiToModel.mapTModel(apiTModel, modelTModel);
943 
944                                 setOperationalInfo(em, modelTModel, publisher);
945 
946                                 em.persist(modelTModel);
947 
948                                 result.getTModel().add(apiTModel);
949                                 changes.add(getChangeRecord(modelTModel, apiTModel, getNode()));
950                                 /*
951                                  //TODO JUDDI-915
952                                  if (obj != null) {
953 
954                                  changes.add(getChangeRecord(modelTModel, apiTModel, node));
955                                  } else {
956                                  //special case for replication, must setup a new data conditional change record
957                                  changes.add(getChangeRecordConditional(modelTModel, apiTModel, node));
958                                  }*/
959 
960                         }
961 
962                         tx.commit();
963                         for (int i = 0; i < changes.size(); i++) {
964                                 ReplicationNotifier.enqueue(changes.get(i));
965                         }
966                         long procTime = System.currentTimeMillis() - startTime;
967                         serviceCounter.update(PublicationQuery.SAVE_TMODEL,
968                                 QueryStatus.SUCCESS, procTime);
969 
970                         return result;
971                 } catch (DispositionReportFaultMessage drfm) {
972                         long procTime = System.currentTimeMillis() - startTime;
973                         serviceCounter.update(PublicationQuery.SAVE_TMODEL,
974                                 QueryStatus.FAILED, procTime);
975                         throw drfm;
976                 } finally {
977                         if (tx.isActive()) {
978                                 tx.rollback();
979                         }
980                         em.close();
981                 }
982         }
983 
984         /**
985          * {@inheritdoc}
986          *
987          */
988         @Override
989         public void setPublisherAssertions(String authInfo,
990                 Holder<List<PublisherAssertion>> publisherAssertion)
991                 throws DispositionReportFaultMessage {
992                 long startTime = System.currentTimeMillis();
993 
994                 EntityManager em = PersistenceManager.getEntityManager();
995                 EntityTransaction tx = em.getTransaction();
996                 List<ChangeRecord> changes = new ArrayList<ChangeRecord>();
997                 try {
998                         tx.begin();
999 
1000                         UddiEntityPublisher publisher = this.getEntityPublisher(em, authInfo);
1001 
1002                         new ValidatePublish(publisher).validateSetPublisherAssertions(em, publisherAssertion);
1003 
1004                         List<?> businessKeysFound = null;
1005                         businessKeysFound = FindBusinessByPublisherQuery.select(em, null, publisher, businessKeysFound);
1006 
1007                         //TODO this has to be reworked to record what was deleted.
1008                         // First, identify all previous assertions that need to be removed
1009                         List<org.apache.juddi.model.PublisherAssertion> existingAssertions = FindPublisherAssertionByBusinessQuery.select(em, businessKeysFound, null);
1010 
1011                         logger.debug(">>>> Existing assertions " + existingAssertions.size() + ", inbound set " + publisherAssertion.value.size());
1012                         List<org.apache.juddi.model.PublisherAssertion> deleteMe = diff(publisherAssertion.value, existingAssertions);
1013                         logger.debug(">>>> DIFF size is " + deleteMe.size());
1014                         for (org.apache.juddi.model.PublisherAssertion del : deleteMe) {
1015                                 logger.debug(">>>> PROCESSING " + del.getBusinessEntityByFromKey().getEntityKey() + " " + del.getBusinessEntityByToKey().getEntityKey());
1016                                 boolean from = false;
1017                                 if (del.getFromCheck() != null) {
1018                                         del.getFromCheck().equalsIgnoreCase("true");
1019                                 }
1020                                 boolean to = false;
1021                                 if (del.getToCheck() != null) {
1022                                         del.getToCheck().equalsIgnoreCase("true");
1023                                 }
1024                                 if (publisher.isOwner(del.getBusinessEntityByFromKey())) {
1025                                         from = false;
1026                                 }
1027                                 if (publisher.isOwner(del.getBusinessEntityByToKey())) {
1028                                         to = false;
1029                                 }
1030                                 PublisherAssertion api = new PublisherAssertion();
1031                                 MappingModelToApi.mapPublisherAssertion(del, api);
1032 
1033                                 if (!to && !from) {
1034                                         logger.debug(">>>> DELETE ME " + del.getBusinessEntityByFromKey().getEntityKey() + " " + del.getBusinessEntityByToKey().getEntityKey());
1035                                         em.remove(del);
1036                                 } else {
1037                                         logger.debug(">>>> MERGING ME " + del.getBusinessEntityByFromKey().getEntityKey() + " " + del.getBusinessEntityByToKey().getEntityKey());
1038                                         del.setFromCheck(from ? "true" : "false");
1039                                         del.setToCheck(to ? "true" : "false");
1040                                         del.setModified(new Date());
1041                                         em.merge(del);
1042                                 }
1043                                 changes.add(getChangeRecord_deletePublisherAssertion(api, getNode(), to, from, System.currentTimeMillis()));
1044                         }
1045                         //DeletePublisherAssertionByBusinessQuery.delete(em, businessKeysFound);
1046 
1047                         // Slate is clean for all assertions involving this publisher, now we simply need to add the new ones (and they will all be "new").
1048                         /*List<org.uddi.api_v3.PublisherAssertion> apiPubAssertionList = publisherAssertion.value;
1049 
1050                         
1051                          for (org.uddi.api_v3.PublisherAssertion apiPubAssertion : apiPubAssertionList) {
1052 
1053                          org.apache.juddi.model.PublisherAssertion modelPubAssertion = new org.apache.juddi.model.PublisherAssertion();
1054 
1055                          MappingApiToModel.mapPublisherAssertion(apiPubAssertion, modelPubAssertion);
1056                                 
1057                          org.apache.juddi.model.BusinessEntity beFrom = em.find(org.apache.juddi.model.BusinessEntity.class, modelPubAssertion.getId().getFromKey());
1058                          org.apache.juddi.model.BusinessEntity beTo = em.find(org.apache.juddi.model.BusinessEntity.class, modelPubAssertion.getId().getToKey());
1059                          modelPubAssertion.setBusinessEntityByFromKey(beFrom);
1060                          modelPubAssertion.setBusinessEntityByToKey(beTo);
1061 
1062                          modelPubAssertion.setFromCheck("false");
1063                          modelPubAssertion.setToCheck("false");
1064 
1065                          if (publisher.isOwner(modelPubAssertion.getBusinessEntityByFromKey())) {
1066                          modelPubAssertion.setFromCheck("true");
1067                          }
1068                          if (publisher.isOwner(modelPubAssertion.getBusinessEntityByToKey())) {
1069                          modelPubAssertion.setToCheck("true");
1070                          }
1071                          em.persist(modelPubAssertion);
1072 
1073                          changes.add(getChangeRecord_NewAssertion(apiPubAssertion, modelPubAssertion, node));
1074 
1075                          }*/
1076                         tx.commit();
1077                         if (!publisherAssertion.value.isEmpty()) {
1078                                 AddPublisherAssertions addPublisherAssertions = new AddPublisherAssertions();
1079                                 addPublisherAssertions.setAuthInfo(authInfo);
1080                                 addPublisherAssertions.getPublisherAssertion().addAll(publisherAssertion.value);
1081                                 addPublisherAssertions(addPublisherAssertions);
1082                         }
1083                         for (int i = 0; i < changes.size(); i++) {
1084                                 ReplicationNotifier.enqueue(changes.get(i));
1085                         }
1086                         long procTime = System.currentTimeMillis() - startTime;
1087                         serviceCounter.update(PublicationQuery.SET_PUBLISHERASSERTIONS,
1088                                 QueryStatus.SUCCESS, procTime);
1089                 } catch (DispositionReportFaultMessage drfm) {
1090                         long procTime = System.currentTimeMillis() - startTime;
1091                         serviceCounter.update(PublicationQuery.SET_PUBLISHERASSERTIONS,
1092                                 QueryStatus.FAILED, procTime);
1093                         throw drfm;
1094                 } finally {
1095                         if (tx.isActive()) {
1096                                 tx.rollback();
1097                         }
1098                         em.close();
1099                 }
1100         }
1101 
1102         private void setOperationalInfo(EntityManager em, org.apache.juddi.model.BusinessEntity uddiEntity, UddiEntityPublisher publisher) throws DispositionReportFaultMessage {
1103 
1104                 uddiEntity.setAuthorizedName(publisher.getAuthorizedName());
1105 
1106                 Date now = new Date();
1107                 uddiEntity.setModified(now);
1108                 uddiEntity.setModifiedIncludingChildren(now);
1109 
1110                 String nodeId = "";
1111                 try {
1112                         nodeId = AppConfig.getConfiguration().getString(Property.JUDDI_NODE_ID);
1113                 } catch (ConfigurationException ce) {
1114                         throw new FatalErrorException(new ErrorMessage("errors.configuration.Retrieval", Property.JUDDI_NODE_ID));
1115                 }
1116                 uddiEntity.setNodeId(nodeId);
1117 
1118                 org.apache.juddi.model.BusinessEntity existingUddiEntity = em.find(uddiEntity.getClass(), uddiEntity.getEntityKey());
1119                 if (existingUddiEntity != null) {
1120                         uddiEntity.setCreated(existingUddiEntity.getCreated());
1121                 } else {
1122                         uddiEntity.setCreated(now);
1123                 }
1124 
1125                 List<org.apache.juddi.model.BusinessService> serviceList = uddiEntity.getBusinessServices();
1126                 for (org.apache.juddi.model.BusinessService service : serviceList) {
1127                         setOperationalInfo(em, service, publisher, true);
1128                 }
1129 
1130                 if (existingUddiEntity != null) {
1131                         em.remove(existingUddiEntity);
1132                 }
1133 
1134         }
1135 
1136         private void setOperationalInfo(EntityManager em, org.apache.juddi.model.BusinessService uddiEntity, UddiEntityPublisher publisher, boolean isChild) throws DispositionReportFaultMessage {
1137 
1138                 uddiEntity.setAuthorizedName(publisher.getAuthorizedName());
1139 
1140                 Date now = new Date();
1141                 uddiEntity.setModified(now);
1142                 uddiEntity.setModifiedIncludingChildren(now);
1143 
1144                 if (!isChild) {
1145                         org.apache.juddi.model.BusinessEntity parent = em.find(org.apache.juddi.model.BusinessEntity.class, uddiEntity.getBusinessEntity().getEntityKey());
1146                         parent.setModifiedIncludingChildren(now);
1147                         em.persist(parent);
1148                 }
1149 
1150                 String nodeId = "";
1151                 try {
1152                         nodeId = AppConfig.getConfiguration().getString(Property.JUDDI_NODE_ID);
1153                 } catch (ConfigurationException ce) {
1154                         throw new FatalErrorException(new ErrorMessage("errors.configuration.Retrieval", Property.JUDDI_NODE_ID));
1155                 }
1156                 uddiEntity.setNodeId(nodeId);
1157 
1158                 org.apache.juddi.model.BusinessService existingUddiEntity = em.find(uddiEntity.getClass(), uddiEntity.getEntityKey());
1159                 if (existingUddiEntity != null) {
1160                         uddiEntity.setCreated(existingUddiEntity.getCreated());
1161                 } else {
1162                         uddiEntity.setCreated(now);
1163                 }
1164 
1165                 List<org.apache.juddi.model.BindingTemplate> bindingList = uddiEntity.getBindingTemplates();
1166                 for (org.apache.juddi.model.BindingTemplate binding : bindingList) {
1167                         setOperationalInfo(em, binding, publisher, true);
1168                 }
1169 
1170                 if (existingUddiEntity != null) {
1171                         em.remove(existingUddiEntity);
1172                 }
1173 
1174         }
1175 
1176         private void setOperationalInfo(EntityManager em, org.apache.juddi.model.BindingTemplate uddiEntity, UddiEntityPublisher publisher, boolean isChild) throws DispositionReportFaultMessage {
1177 
1178                 uddiEntity.setAuthorizedName(publisher.getAuthorizedName());
1179 
1180                 Date now = new Date();
1181                 uddiEntity.setModified(now);
1182                 uddiEntity.setModifiedIncludingChildren(now);
1183 
1184                 //if (!isChild) {
1185                 org.apache.juddi.model.BusinessService parent = em.find(org.apache.juddi.model.BusinessService.class, uddiEntity.getBusinessService().getEntityKey());
1186                 if (parent != null) {
1187                         parent.setModifiedIncludingChildren(now);
1188                         em.persist(parent);
1189 
1190                         // JUDDI-421:  now the businessEntity parent will have it's modifiedIncludingChildren set
1191                         org.apache.juddi.model.BusinessEntity businessParent = em.find(org.apache.juddi.model.BusinessEntity.class, parent.getBusinessEntity().getEntityKey());
1192                         if (businessParent != null) {
1193                                 businessParent.setModifiedIncludingChildren(now);
1194                                 em.persist(businessParent);
1195                         } else {
1196                                 logger.debug("Parent business is null for saved binding template!");
1197                         }
1198                 } else {
1199                         logger.debug("Parent service is null for saved binding template!");
1200                 }
1201                 // }
1202 
1203                 String nodeId = "";
1204                 try {
1205                         nodeId = AppConfig.getConfiguration().getString(Property.JUDDI_NODE_ID);
1206                 } catch (ConfigurationException ce) {
1207                         throw new FatalErrorException(new ErrorMessage("errors.configuration.Retrieval", Property.JUDDI_NODE_ID));
1208                 }
1209                 uddiEntity.setNodeId(nodeId);
1210 
1211                 org.apache.juddi.model.BindingTemplate existingUddiEntity = em.find(uddiEntity.getClass(), uddiEntity.getEntityKey());
1212                 if (existingUddiEntity != null) {
1213                         uddiEntity.setCreated(existingUddiEntity.getCreated());
1214                 } else {
1215                         uddiEntity.setCreated(now);
1216                 }
1217 
1218                 if (existingUddiEntity != null) {
1219                         em.remove(existingUddiEntity);
1220                 }
1221 
1222         }
1223 
1224         private void setOperationalInfo(EntityManager em, org.apache.juddi.model.Tmodel uddiEntity, UddiEntityPublisher publisher) throws DispositionReportFaultMessage {
1225 
1226                 uddiEntity.setAuthorizedName(publisher.getAuthorizedName());
1227 
1228                 Date now = new Date();
1229                 uddiEntity.setModified(now);
1230                 uddiEntity.setModifiedIncludingChildren(now);
1231 
1232                 String nodeId = "";
1233                 try {
1234                         nodeId = AppConfig.getConfiguration().getString(Property.JUDDI_NODE_ID);
1235                 } catch (ConfigurationException ce) {
1236                         throw new FatalErrorException(new ErrorMessage("errors.configuration.Retrieval", Property.JUDDI_NODE_ID));
1237                 }
1238                 uddiEntity.setNodeId(nodeId);
1239 
1240                 org.apache.juddi.model.Tmodel existingUddiEntity = em.find(uddiEntity.getClass(), uddiEntity.getEntityKey());
1241                 if (existingUddiEntity != null) {
1242                         uddiEntity.setCreated(existingUddiEntity.getCreated());
1243                 } else {
1244                         uddiEntity.setCreated(now);
1245                 }
1246 
1247                 if (existingUddiEntity != null) {
1248                         em.remove(existingUddiEntity);
1249                 }
1250 
1251         }
1252 
1253         public static ChangeRecord getChangeRecord(BindingTemplate modelBindingTemplate, org.uddi.api_v3.BindingTemplate api, String node) throws DispositionReportFaultMessage {
1254                 ChangeRecord cr = new ChangeRecord();
1255                 cr.setEntityKey(modelBindingTemplate.getEntityKey());
1256                 cr.setNodeID(node);
1257 
1258                 cr.setRecordType(ChangeRecord.RecordType.ChangeRecordNewData);
1259                 org.uddi.repl_v3.ChangeRecord crapi = new org.uddi.repl_v3.ChangeRecord();
1260                 crapi.setChangeID(new ChangeRecordIDType(node, -1L));
1261                 crapi.setChangeRecordNewData(new ChangeRecordNewData());
1262                 crapi.getChangeRecordNewData().setBindingTemplate(api);
1263                 crapi.getChangeRecordNewData().setOperationalInfo(new OperationalInfo());
1264                 MappingModelToApi.mapOperationalInfo(modelBindingTemplate, crapi.getChangeRecordNewData().getOperationalInfo());
1265                 StringWriter sw = new StringWriter();
1266                 JAXB.marshal(crapi, sw);
1267                 try {
1268                         cr.setContents(sw.toString().getBytes("UTF8"));
1269                 } catch (UnsupportedEncodingException ex) {
1270                         logger.error(ex);
1271                 }
1272                 return cr;
1273         }
1274 
1275         public static ChangeRecord getChangeRecord(BusinessService model, org.uddi.api_v3.BusinessService api, String node) throws DispositionReportFaultMessage {
1276                 ChangeRecord cr = new ChangeRecord();
1277                 cr.setEntityKey(model.getEntityKey());
1278                 cr.setNodeID(node);
1279 
1280                 cr.setRecordType(ChangeRecord.RecordType.ChangeRecordNewData);
1281                 org.uddi.repl_v3.ChangeRecord crapi = new org.uddi.repl_v3.ChangeRecord();
1282                 crapi.setChangeID(new ChangeRecordIDType(node, -1L));
1283                 crapi.setChangeRecordNewData(new ChangeRecordNewData());
1284                 crapi.getChangeRecordNewData().setBusinessService(api);
1285                 crapi.getChangeRecordNewData().setOperationalInfo(new OperationalInfo());
1286                 MappingModelToApi.mapOperationalInfo(model, crapi.getChangeRecordNewData().getOperationalInfo());
1287                 StringWriter sw = new StringWriter();
1288                 JAXB.marshal(crapi, sw);
1289                 try {
1290                         cr.setContents(sw.toString().getBytes("UTF8"));
1291                 } catch (UnsupportedEncodingException ex) {
1292                         logger.error(ex);
1293                 }
1294                 return cr;
1295         }
1296 
1297         public ChangeRecord getChangeRecord_deleteBusiness(String entityKey, String node) {
1298                 ChangeRecord cr = new ChangeRecord();
1299                 cr.setEntityKey(entityKey);
1300                 cr.setNodeID(node);
1301 
1302                 cr.setRecordType(ChangeRecord.RecordType.ChangeRecordDelete);
1303                 org.uddi.repl_v3.ChangeRecord crapi = new org.uddi.repl_v3.ChangeRecord();
1304                 crapi.setChangeID(new ChangeRecordIDType(node, -1L));
1305                 crapi.setChangeRecordDelete(new ChangeRecordDelete());
1306                 crapi.getChangeRecordDelete().setBusinessKey(entityKey);
1307                 crapi.getChangeRecordDelete().setModified(df.newXMLGregorianCalendar(new GregorianCalendar()));
1308 
1309                 StringWriter sw = new StringWriter();
1310                 JAXB.marshal(crapi, sw);
1311                 try {
1312                         cr.setContents(sw.toString().getBytes("UTF8"));
1313                 } catch (UnsupportedEncodingException ex) {
1314                         logger.error(ex);
1315                 }
1316                 return cr;
1317         }
1318 
1319         public ChangeRecord getChangeRecord_deleteService(String entityKey, String node) {
1320                 ChangeRecord cr = new ChangeRecord();
1321                 cr.setEntityKey(entityKey);
1322                 cr.setNodeID(node);
1323 
1324                 cr.setRecordType(ChangeRecord.RecordType.ChangeRecordDelete);
1325                 org.uddi.repl_v3.ChangeRecord crapi = new org.uddi.repl_v3.ChangeRecord();
1326                 crapi.setChangeID(new ChangeRecordIDType(node, -1L));
1327                 crapi.setChangeRecordDelete(new ChangeRecordDelete());
1328                 crapi.getChangeRecordDelete().setServiceKey(entityKey);
1329                 crapi.getChangeRecordDelete().setModified(df.newXMLGregorianCalendar(new GregorianCalendar()));
1330 
1331                 StringWriter sw = new StringWriter();
1332                 JAXB.marshal(crapi, sw);
1333                 try {
1334                         cr.setContents(sw.toString().getBytes("UTF8"));
1335                 } catch (UnsupportedEncodingException ex) {
1336                         logger.error(ex);
1337                 }
1338                 return cr;
1339         }
1340 
1341         /**
1342          * this is for "hiding" a tmodel, not removing it entirely
1343          *
1344          * @param entityKey
1345          * @param node
1346          * @return
1347          */
1348         public ChangeRecord getChangeRecord_deleteTModelHide(String entityKey, String node) {
1349                 ChangeRecord cr = new ChangeRecord();
1350                 cr.setEntityKey(entityKey);
1351                 cr.setNodeID(node);
1352                 cr.setRecordType(ChangeRecord.RecordType.ChangeRecordHide);
1353                 org.uddi.repl_v3.ChangeRecord crapi = new org.uddi.repl_v3.ChangeRecord();
1354                 crapi.setChangeID(new ChangeRecordIDType(node, -1L));
1355 
1356                 crapi.setChangeRecordHide(new ChangeRecordHide());
1357                 crapi.getChangeRecordHide().setTModelKey(entityKey);
1358                 crapi.getChangeRecordHide().setModified(df.newXMLGregorianCalendar(new GregorianCalendar()));
1359 
1360                 StringWriter sw = new StringWriter();
1361                 JAXB.marshal(crapi, sw);
1362                 //JAXB.marshal(crapi, System.out);
1363                 try {
1364                         cr.setContents(sw.toString().getBytes("UTF8"));
1365                 } catch (UnsupportedEncodingException ex) {
1366                         logger.error(ex);
1367                 }
1368                 return cr;
1369         }
1370 
1371         /**
1372          * this is for deleting a tmodel, not hiding it
1373          *
1374          * @param entityKey
1375          * @param node
1376          * @return
1377          */
1378         public static ChangeRecord getChangeRecord_deleteTModelDelete(String entityKey, String node, DatatypeFactory df) {
1379                 ChangeRecord cr = new ChangeRecord();
1380                 cr.setEntityKey(entityKey);
1381                 cr.setNodeID(node);
1382                 cr.setRecordType(ChangeRecord.RecordType.ChangeRecordDelete);
1383                 org.uddi.repl_v3.ChangeRecord crapi = new org.uddi.repl_v3.ChangeRecord();
1384                 crapi.setChangeID(new ChangeRecordIDType(node, -1L));
1385 
1386                 crapi.setChangeRecordDelete(new ChangeRecordDelete());
1387                 crapi.getChangeRecordDelete().setTModelKey(entityKey);
1388                 crapi.getChangeRecordDelete().setModified(df.newXMLGregorianCalendar(new GregorianCalendar()));
1389 
1390                 StringWriter sw = new StringWriter();
1391                 JAXB.marshal(crapi, sw);
1392                 //JAXB.marshal(crapi, System.out);
1393                 try {
1394                         cr.setContents(sw.toString().getBytes("UTF8"));
1395                 } catch (UnsupportedEncodingException ex) {
1396                         logger.error(ex);
1397                 }
1398                 return cr;
1399         }
1400 
1401         public static ChangeRecord getChangeRecord(BusinessEntity modelBusinessEntity, org.uddi.api_v3.BusinessEntity apiBusinessEntity, String node) throws DispositionReportFaultMessage {
1402                 ChangeRecord cr = new ChangeRecord();
1403                 cr.setEntityKey(modelBusinessEntity.getEntityKey());
1404                 cr.setNodeID(node);
1405 
1406                 cr.setRecordType(ChangeRecord.RecordType.ChangeRecordNewData);
1407                 org.uddi.repl_v3.ChangeRecord crapi = new org.uddi.repl_v3.ChangeRecord();
1408                 crapi.setChangeID(new ChangeRecordIDType(node, -1L));
1409                 crapi.setChangeRecordNewData(new ChangeRecordNewData());
1410                 crapi.getChangeRecordNewData().setBusinessEntity(apiBusinessEntity);
1411                 crapi.getChangeRecordNewData().setOperationalInfo(new OperationalInfo());
1412                 MappingModelToApi.mapOperationalInfo(modelBusinessEntity, crapi.getChangeRecordNewData().getOperationalInfo());
1413                 StringWriter sw = new StringWriter();
1414                 JAXB.marshal(crapi, sw);
1415                 try {
1416                         cr.setContents(sw.toString().getBytes("UTF8"));
1417                 } catch (UnsupportedEncodingException ex) {
1418                         logger.error(ex);
1419                 }
1420                 return cr;
1421         }
1422 
1423         public static ChangeRecord getChangeRecord(Tmodel modelBusinessEntity, org.uddi.api_v3.TModel apiBusinessEntity, String node) throws DispositionReportFaultMessage {
1424                 ChangeRecord cr = new ChangeRecord();
1425                 if (!apiBusinessEntity.getTModelKey().equals(modelBusinessEntity.getEntityKey())) {
1426                         throw new FatalErrorException(new ErrorMessage("E_fatalError", "the model and api keys do not match when saving a tmodel!"));
1427                 }
1428                 cr.setEntityKey(modelBusinessEntity.getEntityKey());
1429                 cr.setNodeID(node);
1430 
1431                 cr.setRecordType(ChangeRecord.RecordType.ChangeRecordNewData);
1432                 org.uddi.repl_v3.ChangeRecord crapi = new org.uddi.repl_v3.ChangeRecord();
1433                 crapi.setChangeID(new ChangeRecordIDType(node, -1L));
1434                 crapi.setChangeRecordNewData(new ChangeRecordNewData());
1435                 crapi.getChangeRecordNewData().setTModel(apiBusinessEntity);
1436                 crapi.getChangeRecordNewData().getTModel().setTModelKey(modelBusinessEntity.getEntityKey());
1437                 crapi.getChangeRecordNewData().setOperationalInfo(new OperationalInfo());
1438                 MappingModelToApi.mapOperationalInfo(modelBusinessEntity, crapi.getChangeRecordNewData().getOperationalInfo());
1439                 StringWriter sw = new StringWriter();
1440                 JAXB.marshal(crapi, sw);
1441                 try {
1442                         cr.setContents(sw.toString().getBytes("UTF8"));
1443                 } catch (UnsupportedEncodingException ex) {
1444                         logger.error(ex);
1445                 }
1446                 return cr;
1447         }
1448 
1449         public ChangeRecord getChangeRecord_deleteBinding(String entityKey, String node) {
1450                 ChangeRecord cr = new ChangeRecord();
1451                 cr.setEntityKey(entityKey);
1452                 cr.setNodeID(node);
1453 
1454                 cr.setRecordType(ChangeRecord.RecordType.ChangeRecordDelete);
1455                 org.uddi.repl_v3.ChangeRecord crapi = new org.uddi.repl_v3.ChangeRecord();
1456                 crapi.setChangeID(new ChangeRecordIDType(node, -1L));
1457                 crapi.setChangeRecordDelete(new ChangeRecordDelete());
1458                 crapi.getChangeRecordDelete().setBindingKey(entityKey);
1459                 crapi.getChangeRecordDelete().setModified(df.newXMLGregorianCalendar(new GregorianCalendar()));
1460 
1461                 StringWriter sw = new StringWriter();
1462                 JAXB.marshal(crapi, sw);
1463                 try {
1464                         cr.setContents(sw.toString().getBytes("UTF8"));
1465                 } catch (UnsupportedEncodingException ex) {
1466                         logger.error(ex);
1467                 }
1468                 return cr;
1469         }
1470 
1471         public ChangeRecord getChangeRecord_deletePublisherAssertion(PublisherAssertion entity, String node, boolean ToBusinessDelete, boolean FromBusinessDelete, long timestamp) {
1472                 ChangeRecord cr = new ChangeRecord();
1473 
1474                 cr.setNodeID(node);
1475 
1476                 cr.setRecordType(ChangeRecord.RecordType.ChangeRecordDeleteAssertion);
1477                 org.uddi.repl_v3.ChangeRecord crapi = new org.uddi.repl_v3.ChangeRecord();
1478                 crapi.setChangeID(new ChangeRecordIDType(node, -1L));
1479                 crapi.setChangeRecordDeleteAssertion(new ChangeRecordDeleteAssertion());
1480                 crapi.getChangeRecordDeleteAssertion().setPublisherAssertion(entity);
1481                 crapi.getChangeRecordDeleteAssertion().setToBusinessCheck(ToBusinessDelete);
1482                 crapi.getChangeRecordDeleteAssertion().setFromBusinessCheck(FromBusinessDelete);
1483                 GregorianCalendar gcal = new GregorianCalendar();
1484                 gcal.setTimeInMillis(timestamp);
1485                 crapi.getChangeRecordDeleteAssertion().setModified(df.newXMLGregorianCalendar(gcal));
1486 
1487                 StringWriter sw = new StringWriter();
1488                 JAXB.marshal(crapi, sw);
1489                 try {
1490                         cr.setContents(sw.toString().getBytes("UTF8"));
1491                 } catch (UnsupportedEncodingException ex) {
1492                         logger.error(ex);
1493                 }
1494                 return cr;
1495         }
1496 
1497         public ChangeRecord getChangeRecord_NewAssertion(PublisherAssertion apiPubAssertion, org.apache.juddi.model.PublisherAssertion modelPubAssertion, String node) {
1498                 ChangeRecord cr = new ChangeRecord();
1499 
1500                 cr.setNodeID(node);
1501 
1502                 cr.setRecordType(ChangeRecord.RecordType.ChangeRecordPublisherAssertion);
1503                 org.uddi.repl_v3.ChangeRecord crapi = new org.uddi.repl_v3.ChangeRecord();
1504                 crapi.setChangeID(new ChangeRecordIDType(node, -1L));
1505                 crapi.setChangeRecordPublisherAssertion(new ChangeRecordPublisherAssertion());
1506                 crapi.getChangeRecordPublisherAssertion().setFromBusinessCheck(modelPubAssertion.getFromCheck().equalsIgnoreCase("true"));
1507                 crapi.getChangeRecordPublisherAssertion().setToBusinessCheck(modelPubAssertion.getToCheck().equalsIgnoreCase("true"));
1508                 crapi.getChangeRecordPublisherAssertion().setPublisherAssertion(apiPubAssertion);
1509 
1510                 crapi.getChangeRecordPublisherAssertion().setModified(df.newXMLGregorianCalendar(new GregorianCalendar()));
1511 
1512                 StringWriter sw = new StringWriter();
1513                 JAXB.marshal(crapi, sw);
1514                 try {
1515                         cr.setContents(sw.toString().getBytes("UTF8"));
1516                 } catch (UnsupportedEncodingException ex) {
1517                         logger.error(ex);
1518                 }
1519                 return cr;
1520         }
1521 
1522         /**
1523          *
1524          * @param value keep these
1525          * @param existingAssertions return a list of these that are NOT in
1526          * 'value'
1527          * @return
1528          * @throws DispositionReportFaultMessage
1529          */
1530         private List<org.apache.juddi.model.PublisherAssertion> diff(List<PublisherAssertion> value, List<org.apache.juddi.model.PublisherAssertion> existingAssertions) throws DispositionReportFaultMessage {
1531                 List<org.apache.juddi.model.PublisherAssertion> ret = new ArrayList<org.apache.juddi.model.PublisherAssertion>();
1532                 if (value == null || value.isEmpty()) {
1533                         return existingAssertions;
1534                 }
1535                 if (existingAssertions == null) {
1536                         return new ArrayList<org.apache.juddi.model.PublisherAssertion>();
1537                 }
1538                 for (org.apache.juddi.model.PublisherAssertion model : existingAssertions) {
1539 
1540                         boolean found = false;
1541                         for (PublisherAssertion paapi : value) {
1542                                 if (model.getBusinessEntityByFromKey().getEntityKey().equalsIgnoreCase(paapi.getFromKey())
1543                                         && model.getBusinessEntityByToKey().getEntityKey().equalsIgnoreCase(paapi.getToKey())
1544                                         && model.getKeyName().equals(paapi.getKeyedReference().getKeyName())
1545                                         && model.getKeyValue().equals(paapi.getKeyedReference().getKeyValue())
1546                                         && model.getTmodelKey().equalsIgnoreCase(paapi.getKeyedReference().getTModelKey())) {
1547                                         found = true;
1548                                         break;
1549                                 }
1550                         }
1551                         if (!found) {
1552                                 ret.add(model);
1553                         }
1554                 }
1555                 return ret;
1556         }
1557 
1558         private static ChangeRecord getChangeRecordConditional(Tmodel modelTModel, TModel apiTModel, String node) throws DispositionReportFaultMessage {
1559                 ChangeRecord cr = new ChangeRecord();
1560                 if (!apiTModel.getTModelKey().equals(modelTModel.getEntityKey())) {
1561                         throw new FatalErrorException(new ErrorMessage("E_fatalError", "the model and api keys do not match when saving a tmodel!"));
1562                 }
1563                 cr.setEntityKey(modelTModel.getEntityKey());
1564                 cr.setNodeID(node);
1565 
1566                 cr.setRecordType(ChangeRecord.RecordType.ChangeRecordNewDataConditional);
1567                 org.uddi.repl_v3.ChangeRecord crapi = new org.uddi.repl_v3.ChangeRecord();
1568                 crapi.setChangeID(new ChangeRecordIDType(node, -1L));
1569                 crapi.setChangeRecordNewDataConditional(new ChangeRecordNewDataConditional());
1570                 crapi.getChangeRecordNewDataConditional().setChangeRecordNewData(new ChangeRecordNewData());
1571                 crapi.getChangeRecordNewDataConditional().getChangeRecordNewData().setTModel(apiTModel);
1572                 crapi.getChangeRecordNewDataConditional().getChangeRecordNewData().getTModel().setTModelKey(modelTModel.getEntityKey());
1573                 crapi.getChangeRecordNewDataConditional().getChangeRecordNewData().setOperationalInfo(new OperationalInfo());
1574                 MappingModelToApi.mapOperationalInfo(modelTModel, crapi.getChangeRecordNewDataConditional().getChangeRecordNewData().getOperationalInfo());
1575                 StringWriter sw = new StringWriter();
1576                 JAXB.marshal(crapi, sw);
1577                 try {
1578                         cr.setContents(sw.toString().getBytes("UTF8"));
1579                 } catch (UnsupportedEncodingException ex) {
1580                         logger.error(ex);
1581                 }
1582                 return cr;
1583         }
1584 
1585         private void removeExistingPublisherAssertionSignatures(String from, String to, EntityManager em) {
1586                 Query createQuery = em.createQuery("delete from Signature pa where pa.publisherAssertionFromKey=:from and pa.publisherAssertionToKey=:to");
1587                 createQuery.setParameter("from", from);
1588                 createQuery.setParameter("to", to);
1589                 createQuery.executeUpdate();
1590         }
1591 
1592         private void savePushliserAssertionSignatures(String from, String to, List<Signature> signatures, EntityManager em) {
1593                 if (signatures == null) {
1594                         return;
1595                 }
1596                 for (Signature s : signatures) {
1597                         s.setPublisherAssertionFromKey(from);
1598                         s.setPublisherAssertionToKey(to);
1599                         em.persist(s);
1600                 }
1601         }
1602 
1603 }