View Javadoc
1   /**
2    * Copyright 2005-2014 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.krad.service.impl;
17  
18  import org.kuali.rice.krad.data.provider.annotation.SerializationContext;
19  import org.kuali.rice.krad.data.provider.annotation.Serialized;
20  import org.kuali.rice.krad.service.BusinessObjectSerializerService;
21  import org.kuali.rice.krad.util.documentserializer.PropertySerializabilityEvaluator;
22  import org.kuali.rice.krad.util.documentserializer.PropertySerializabilityEvaluatorBase;
23  import org.kuali.rice.krad.util.documentserializer.SerializationState;
24  
25  import javax.persistence.Transient;
26  import java.lang.reflect.Field;
27  
28  /**
29   * @author Kuali Rice Team (rice.collab@kuali.org)
30   */
31  public class DataObjectSerializerServiceImpl extends SerializerServiceBase implements BusinessObjectSerializerService {
32  
33      /**
34       * {@inheritDoc}
35       */
36      @Override
37      public PropertySerializabilityEvaluator getPropertySerizabilityEvaluator(Object businessObject) {
38          // Avoiding AlwaysTruePropertySerializibilityEvaluator otherwise SerializerServiceBase uses the
39          // getXmlObjectSerializerService instead of it's own XStream instance, and our ignoreField method isn't used.
40          PropertySerializabilityEvaluator evaluator = new PropertySerializabilityEvaluatorBase() {
41              @Override
42              public boolean isPropertySerializable(SerializationState state, Object containingObject,
43                      String childPropertyName, Object childPropertyValue) {
44                  return true;
45              }
46          };
47  
48          return evaluator;
49      }
50  
51      /**
52       * Examines {@link Serialized} and {@link Transient} annotations to determine if the field should not be serialized.
53       *
54       * <p>{@inheritDoc}</p>
55       */
56      @Override
57      protected boolean ignoreField(Field field) {
58          Serialized serialized = field.getAnnotation(Serialized.class);
59  
60          // if we have a @Serialized annotation that is relevant to serializationContext, let it determine serializability
61          if (serialized != null && SerializationContext.MAINTENANCE.matches(serialized.forContexts())) {
62              return !serialized.enabled(); // note the ! operator, since ignore=true is equiv to serialized=false
63          }
64  
65          // otherwise, if the field is marked as javax.persistence.Transient, ignore it
66          if (field.getAnnotation(Transient.class) != null) {
67              return true;
68          }
69  
70          // by default we don't want to ignore it
71          return false;
72      }
73  }