001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krad.service.impl;
017    
018    import org.kuali.rice.krad.document.Document;
019    import org.kuali.rice.krad.service.DocumentSerializerService;
020    import org.kuali.rice.krad.service.XmlObjectSerializerService;
021    import org.kuali.rice.krad.util.documentserializer.AlwaysTruePropertySerializibilityEvaluator;
022    import org.kuali.rice.krad.util.documentserializer.PropertySerializabilityEvaluator;
023    import org.kuali.rice.krad.util.documentserializer.SerializationState;
024    
025    /**
026     * Default implementation of the {@link DocumentSerializerService}.  If no <workflowProperties> have been defined in the
027     * data dictionary for a document type (i.e. {@link Document#getDocumentPropertySerizabilityEvaluator()} returns an instance of 
028     * {@link AlwaysTruePropertySerializibilityEvaluator}), then this service will revert to using the {@link XmlObjectSerializerService}
029     * bean, which was the old way of serializing a document for routing.  If workflowProperties are defined, then this implementation
030     * will selectively serialize items.
031     */
032    public class DocumentSerializerServiceImpl extends SerializerServiceBase implements DocumentSerializerService {
033        
034        /**
035         * Serializes a document for routing
036         * 
037         * @see org.kuali.rice.krad.service.DocumentSerializerService#serializeDocumentToXmlForRouting(org.kuali.rice.krad.document.Document)
038         */
039        public String serializeDocumentToXmlForRouting(Document document) {
040            PropertySerializabilityEvaluator propertySerizabilityEvaluator = document.getDocumentPropertySerizabilityEvaluator();
041            evaluators.set(propertySerizabilityEvaluator);
042            SerializationState state = createNewDocumentSerializationState(document);
043            serializationStates.set(state);
044            
045            Object xmlWrapper = wrapDocumentWithMetadata(document);
046            String xml;
047            if (propertySerizabilityEvaluator instanceof AlwaysTruePropertySerializibilityEvaluator) {
048                xml = getXmlObjectSerializerService().toXml(xmlWrapper);
049            }
050            else {
051                xml = xstream.toXML(xmlWrapper);
052            }
053            
054            evaluators.set(null);
055            serializationStates.set(null);
056            return xml;
057        }
058    
059        /**
060         * Wraps the document before it is routed.  This implementation defers to {@link Document#wrapDocumentWithMetadataForXmlSerialization()}.
061         * 
062         * @param document
063         * @return may return the document, or may return another object that wraps around the document to provide additional metadata
064         */
065        protected Object wrapDocumentWithMetadata(Document document) {
066            return document.wrapDocumentWithMetadataForXmlSerialization();
067        }
068        
069    }