View Javadoc

1   /*
2    * Copyright 2007-2009 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kns.service.impl;
17  
18  import java.util.List;
19  
20  import org.kuali.rice.kns.bo.BusinessObject;
21  import org.kuali.rice.kns.datadictionary.MaintainableSectionDefinition;
22  import org.kuali.rice.kns.datadictionary.MaintenanceDocumentEntry;
23  import org.kuali.rice.kns.service.BusinessObjectSerializerService;
24  import org.kuali.rice.kns.service.KNSServiceLocator;
25  import org.kuali.rice.kns.service.MaintenanceDocumentDictionaryService;
26  import org.kuali.rice.kns.util.documentserializer.AlwaysTruePropertySerializibilityEvaluator;
27  import org.kuali.rice.kns.util.documentserializer.MaintenanceDocumentPropertySerializibilityEvaluator;
28  import org.kuali.rice.kns.util.documentserializer.PropertySerializabilityEvaluator;
29  import org.kuali.rice.kns.util.documentserializer.SerializationState;
30  
31  public class BusinessObjectSerializerServiceImpl extends SerializerServiceBase implements BusinessObjectSerializerService {
32  
33      /**
34       * Serializes a document for routing
35       * 
36       * @see org.kuali.rice.kns.service.DocumentSerializerService#serializeDocumentToXml(org.kuali.rice.kns.document.Document)
37       */
38      public String serializeBusinessObjectToXml(BusinessObject businessObject) {
39          PropertySerializabilityEvaluator propertySerizabilityEvaluator = getPropertySerizabilityEvaluator(businessObject);
40          evaluators.set(propertySerizabilityEvaluator);
41          SerializationState state = new SerializationState(); //createNewDocumentSerializationState(document);
42          serializationStates.set(state);
43          
44          //Object xmlWrapper = null;//wrapDocumentWithMetadata(document);
45          String xml;
46          if (propertySerizabilityEvaluator instanceof AlwaysTruePropertySerializibilityEvaluator) {
47              xml = getXmlObjectSerializerService().toXml(businessObject);
48          }
49          else {
50              xml = xstream.toXML(businessObject);
51          }
52          
53          evaluators.set(null);
54          serializationStates.set(null);
55          return xml;
56      }
57  
58      public PropertySerializabilityEvaluator getPropertySerizabilityEvaluator(BusinessObject businessObject) {
59          MaintenanceDocumentDictionaryService maintenanceDocumentDictionaryService = 
60          	KNSServiceLocator.getMaintenanceDocumentDictionaryService();
61          String docTypeName = maintenanceDocumentDictionaryService.getDocumentTypeName(businessObject.getClass());
62          MaintenanceDocumentEntry maintenanceDocumentEntry = maintenanceDocumentDictionaryService.getMaintenanceDocumentEntry(docTypeName);
63          List<MaintainableSectionDefinition> maintainableSectionDefinitions = maintenanceDocumentEntry.getMaintainableSections();
64          if (maintainableSectionDefinitions == null) {
65              return new AlwaysTruePropertySerializibilityEvaluator();
66          }
67          else {
68              PropertySerializabilityEvaluator evaluator = new MaintenanceDocumentPropertySerializibilityEvaluator();
69              evaluator.initializeEvaluator(businessObject);
70              return evaluator;
71          }
72      }
73  }