001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.krad.service.impl;
017
018
019import org.kuali.rice.krad.datadictionary.MaintenanceDocumentEntry;
020import org.kuali.rice.krad.service.BusinessObjectSerializerService;
021import org.kuali.rice.krad.service.DocumentDictionaryService;
022import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
023import org.kuali.rice.krad.util.documentserializer.AlwaysTruePropertySerializibilityEvaluator;
024import org.kuali.rice.krad.util.documentserializer.PropertySerializabilityEvaluator;
025import org.kuali.rice.krad.util.documentserializer.SerializationState;
026
027public class BusinessObjectSerializerServiceImpl extends SerializerServiceBase implements BusinessObjectSerializerService {
028
029    private DocumentDictionaryService documentDictionaryService;
030
031    /**
032     * Serializes a document for routing
033     *
034     * @see org.kuali.rice.krad.service.DocumentSerializerService#serializeDocumentToXml(org.kuali.rice.krad.document.Document)
035     */
036    public String serializeBusinessObjectToXml(Object businessObject) {
037        PropertySerializabilityEvaluator propertySerizabilityEvaluator =
038                getPropertySerizabilityEvaluator(businessObject);
039        evaluators.set(propertySerizabilityEvaluator);
040        SerializationState state = new SerializationState(); //createNewDocumentSerializationState(document);
041        serializationStates.set(state);
042
043        //Object xmlWrapper = null;//wrapDocumentWithMetadata(document);
044        String xml;
045        if (propertySerizabilityEvaluator instanceof AlwaysTruePropertySerializibilityEvaluator) {
046            xml = getXmlObjectSerializerService().toXml(businessObject);
047        } else {
048            xml = xstream.toXML(businessObject);
049        }
050
051        evaluators.set(null);
052        serializationStates.set(null);
053        return xml;
054    }
055
056    public PropertySerializabilityEvaluator getPropertySerizabilityEvaluator(Object businessObject) {
057        PropertySerializabilityEvaluator evaluator = null;
058
059        String docTypeName = getDocumentDictionaryService().getMaintenanceDocumentTypeName(businessObject.getClass());
060        MaintenanceDocumentEntry maintenanceDocumentEntry =
061                getDocumentDictionaryService().getMaintenanceDocumentEntry(docTypeName);
062
063        // TODO: need to determine view properties to serialize
064        evaluator = new AlwaysTruePropertySerializibilityEvaluator();
065
066        return evaluator;
067    }
068
069    protected DocumentDictionaryService getDocumentDictionaryService() {
070        if (documentDictionaryService == null) {
071            this.documentDictionaryService = KRADServiceLocatorWeb.getDocumentDictionaryService();
072        }
073        return documentDictionaryService;
074    }
075
076    public void setDocumentDictionaryService(DocumentDictionaryService documentDictionaryService) {
077        this.documentDictionaryService = documentDictionaryService;
078    }
079}