View Javadoc
1   package org.kuali.ole.converters;
2   
3   import com.thoughtworks.xstream.converters.Converter;
4   import com.thoughtworks.xstream.converters.MarshallingContext;
5   import com.thoughtworks.xstream.converters.UnmarshallingContext;
6   import com.thoughtworks.xstream.io.HierarchicalStreamReader;
7   import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
8   import org.apache.commons.collections.CollectionUtils;
9   import org.apache.commons.lang.StringEscapeUtils;
10  import org.apache.commons.lang3.StringUtils;
11  import org.kuali.ole.bo.serachRetrieve.OleSRUCirculationDocument;
12  import org.kuali.ole.bo.serachRetrieve.OleSRUInstanceDocument;
13  import org.kuali.ole.bo.serachRetrieve.OleSRUInstanceVolume;
14  import org.springframework.util.ReflectionUtils;
15  
16  import java.lang.reflect.Method;
17  import java.lang.reflect.Modifier;
18  import java.util.ArrayList;
19  
20  /**
21   * Created with IntelliJ IDEA.
22   * User: aurojyotit
23   * Date: 1/13/15
24   * Time: 7:13 PM
25   * To change this template use File | Settings | File Templates.
26   */
27  public class OleSRUInstanceDocumentConverter implements Converter {
28      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleSRUInstanceDocumentConverter.class);
29  
30      @Override
31      public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
32          OleSRUInstanceDocument oleSRUInstanceDocument=(OleSRUInstanceDocument)source;
33          convertToEscape(oleSRUInstanceDocument);
34          if (CollectionUtils.isNotEmpty(oleSRUInstanceDocument.getVolumes())) {
35              for (OleSRUInstanceVolume oleSRUInstanceVolume : oleSRUInstanceDocument.getVolumes()) {
36                  convertToEscape(oleSRUInstanceVolume);
37              }
38          }
39          if (CollectionUtils.isNotEmpty(oleSRUInstanceDocument.getCirculations())) {
40              for (OleSRUCirculationDocument oleSRUCirculationDocument : oleSRUInstanceDocument.getCirculations()) {
41                  convertToEscape(oleSRUCirculationDocument);
42              }
43          }
44  
45  
46      }
47  
48      @Override
49      public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
50          OleSRUInstanceDocument oleSRUInstanceDocument=new OleSRUInstanceDocument();
51          return oleSRUInstanceDocument;
52      }
53  
54      @Override
55      public boolean canConvert(Class type) {
56          return type.equals(OleSRUInstanceDocument.class);
57      }
58  
59       public void convertToEscape(Object obj) {
60           Class clazz=obj.getClass();
61           Method[] methods = clazz.getDeclaredMethods();
62           for (Method method : methods) {
63               boolean isSetter= Modifier.isPublic(method.getModifiers()) &&
64                       method.getReturnType()!=null && StringUtils.isNotEmpty(method.getReturnType().getName()) && method.getReturnType().getName().equalsIgnoreCase("void") &&
65                       method.getName().matches("^set[A-Z].*");
66               Class<?>[] parameterTypes=method.getParameterTypes();
67  
68               if (isSetter) {
69                   if (parameterTypes.length == 1 && parameterTypes[0].getName().equalsIgnoreCase("java.lang.String")) {
70                       try {
71                           String value = (String) method.getDefaultValue();
72                           if(StringUtils.isNotBlank(value)){
73                               value = StringEscapeUtils.unescapeXml(value);
74                               method.invoke(obj, value);
75                           }
76  
77                       } catch (Exception e) {
78                           LOG.error("Error :while converting data to unescapeXml "+e);
79                       }
80                   }
81               }
82  
83           }
84       }
85  }