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
018import org.kuali.rice.krad.data.provider.annotation.SerializationContext;
019import org.kuali.rice.krad.data.provider.annotation.Serialized;
020import org.kuali.rice.krad.service.BusinessObjectSerializerService;
021import org.kuali.rice.krad.util.documentserializer.PropertySerializabilityEvaluator;
022import org.kuali.rice.krad.util.documentserializer.PropertySerializabilityEvaluatorBase;
023import org.kuali.rice.krad.util.documentserializer.SerializationState;
024
025import javax.persistence.Transient;
026import java.lang.reflect.Field;
027
028/**
029 * @author Kuali Rice Team (rice.collab@kuali.org)
030 */
031public class DataObjectSerializerServiceImpl extends SerializerServiceBase implements BusinessObjectSerializerService {
032
033    /**
034     * {@inheritDoc}
035     */
036    @Override
037    public PropertySerializabilityEvaluator getPropertySerizabilityEvaluator(Object businessObject) {
038        // Avoiding AlwaysTruePropertySerializibilityEvaluator otherwise SerializerServiceBase uses the
039        // getXmlObjectSerializerService instead of it's own XStream instance, and our ignoreField method isn't used.
040        PropertySerializabilityEvaluator evaluator = new PropertySerializabilityEvaluatorBase() {
041            @Override
042            public boolean isPropertySerializable(SerializationState state, Object containingObject,
043                    String childPropertyName, Object childPropertyValue) {
044                return true;
045            }
046        };
047
048        return evaluator;
049    }
050
051    /**
052     * Examines {@link Serialized} and {@link Transient} annotations to determine if the field should not be serialized.
053     *
054     * <p>{@inheritDoc}</p>
055     */
056    @Override
057    protected boolean ignoreField(Field field) {
058        Serialized serialized = field.getAnnotation(Serialized.class);
059
060        // if we have a @Serialized annotation that is relevant to serializationContext, let it determine serializability
061        if (serialized != null && SerializationContext.MAINTENANCE.matches(serialized.forContexts())) {
062            return !serialized.enabled(); // note the ! operator, since ignore=true is equiv to serialized=false
063        }
064
065        // otherwise, if the field is marked as javax.persistence.Transient, ignore it
066        if (field.getAnnotation(Transient.class) != null) {
067            return true;
068        }
069
070        // by default we don't want to ignore it
071        return false;
072    }
073}