001package org.kuali.ole.converters;
002
003import com.thoughtworks.xstream.converters.Converter;
004import com.thoughtworks.xstream.converters.MarshallingContext;
005import com.thoughtworks.xstream.converters.UnmarshallingContext;
006import com.thoughtworks.xstream.io.HierarchicalStreamReader;
007import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
008import org.apache.commons.collections.CollectionUtils;
009import org.apache.commons.lang.StringEscapeUtils;
010import org.apache.commons.lang3.StringUtils;
011import org.kuali.ole.bo.serachRetrieve.OleSRUCirculationDocument;
012import org.kuali.ole.bo.serachRetrieve.OleSRUInstanceDocument;
013import org.kuali.ole.bo.serachRetrieve.OleSRUInstanceVolume;
014import org.springframework.util.ReflectionUtils;
015
016import java.lang.reflect.Method;
017import java.lang.reflect.Modifier;
018import java.util.ArrayList;
019
020/**
021 * Created with IntelliJ IDEA.
022 * User: aurojyotit
023 * Date: 1/13/15
024 * Time: 7:13 PM
025 * To change this template use File | Settings | File Templates.
026 */
027public class OleSRUInstanceDocumentConverter implements Converter {
028    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleSRUInstanceDocumentConverter.class);
029
030    @Override
031    public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
032        OleSRUInstanceDocument oleSRUInstanceDocument=(OleSRUInstanceDocument)source;
033        convertToEscape(oleSRUInstanceDocument);
034        if (CollectionUtils.isNotEmpty(oleSRUInstanceDocument.getVolumes())) {
035            for (OleSRUInstanceVolume oleSRUInstanceVolume : oleSRUInstanceDocument.getVolumes()) {
036                convertToEscape(oleSRUInstanceVolume);
037            }
038        }
039        if (CollectionUtils.isNotEmpty(oleSRUInstanceDocument.getCirculations())) {
040            for (OleSRUCirculationDocument oleSRUCirculationDocument : oleSRUInstanceDocument.getCirculations()) {
041                convertToEscape(oleSRUCirculationDocument);
042            }
043        }
044
045
046    }
047
048    @Override
049    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
050        OleSRUInstanceDocument oleSRUInstanceDocument=new OleSRUInstanceDocument();
051        return oleSRUInstanceDocument;
052    }
053
054    @Override
055    public boolean canConvert(Class type) {
056        return type.equals(OleSRUInstanceDocument.class);
057    }
058
059     public void convertToEscape(Object obj) {
060         Class clazz=obj.getClass();
061         Method[] methods = clazz.getDeclaredMethods();
062         for (Method method : methods) {
063             boolean isSetter= Modifier.isPublic(method.getModifiers()) &&
064                     method.getReturnType()!=null && StringUtils.isNotEmpty(method.getReturnType().getName()) && method.getReturnType().getName().equalsIgnoreCase("void") &&
065                     method.getName().matches("^set[A-Z].*");
066             Class<?>[] parameterTypes=method.getParameterTypes();
067
068             if (isSetter) {
069                 if (parameterTypes.length == 1 && parameterTypes[0].getName().equalsIgnoreCase("java.lang.String")) {
070                     try {
071                         String value = (String) method.getDefaultValue();
072                         if(StringUtils.isNotBlank(value)){
073                             value = StringEscapeUtils.unescapeXml(value);
074                             method.invoke(obj, value);
075                         }
076
077                     } catch (Exception e) {
078                         LOG.error("Error :while converting data to unescapeXml "+e);
079                     }
080                 }
081             }
082
083         }
084     }
085}