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.kuali.ole.bo.explain.OleSRUExplainServerInfo;
009
010
011/**
012 * Created with IntelliJ IDEA.
013 * User: ?
014 * Date: 7/19/12
015 * Time: 7:02 PM
016 * To change this template use File | Settings | File Templates.
017 */
018public class OleSRUExplainServerInfoConverter implements Converter {
019
020
021    @Override
022    public void marshal(Object o, HierarchicalStreamWriter hierarchicalStreamWriter,
023                        MarshallingContext marshallingContext) {
024        OleSRUExplainServerInfo oleSRUExplainServerInfo = (OleSRUExplainServerInfo) o;
025        hierarchicalStreamWriter.addAttribute("protocol", oleSRUExplainServerInfo.getProtocol() == null ? "" : oleSRUExplainServerInfo.getProtocol());
026        hierarchicalStreamWriter.addAttribute("version", oleSRUExplainServerInfo.getVersion() == null ? "" : oleSRUExplainServerInfo.getVersion());
027        hierarchicalStreamWriter.addAttribute("transport", oleSRUExplainServerInfo.getTransport() == null ? "" : oleSRUExplainServerInfo.getTransport());
028        hierarchicalStreamWriter.addAttribute("method", oleSRUExplainServerInfo.getMethod() == null ? "" : oleSRUExplainServerInfo.getMethod());
029        hierarchicalStreamWriter.setValue(oleSRUExplainServerInfo.getValue());
030        hierarchicalStreamWriter.startNode("zr:host");
031        hierarchicalStreamWriter.setValue(oleSRUExplainServerInfo.getHost());
032        hierarchicalStreamWriter.endNode();
033        hierarchicalStreamWriter.startNode("zr:port");
034        hierarchicalStreamWriter.setValue(oleSRUExplainServerInfo.getPort());
035        hierarchicalStreamWriter.endNode();
036        hierarchicalStreamWriter.startNode("zr:database");
037        hierarchicalStreamWriter.setValue(oleSRUExplainServerInfo.getDatabase());
038        hierarchicalStreamWriter.endNode();
039
040    }
041
042    @Override
043    public Object unmarshal(HierarchicalStreamReader hierarchicalStreamReader,
044                            UnmarshallingContext unmarshallingContext) {
045        // System.out.println("datafield unmarshal");
046        OleSRUExplainServerInfo oleSRUExplainServerInfo = new OleSRUExplainServerInfo();
047        oleSRUExplainServerInfo.setProtocol(hierarchicalStreamReader.getAttribute("protocol"));
048        oleSRUExplainServerInfo.setVersion(hierarchicalStreamReader.getAttribute("version"));
049        oleSRUExplainServerInfo.setTransport(hierarchicalStreamReader.getAttribute("transport"));
050        oleSRUExplainServerInfo.setMethod(hierarchicalStreamReader.getAttribute("method"));
051        oleSRUExplainServerInfo.setValue(hierarchicalStreamReader.getValue());
052        while (hierarchicalStreamReader.hasMoreChildren()) {
053            hierarchicalStreamReader.moveDown();
054            if ("zr:host".equals(hierarchicalStreamReader.getNodeName()))
055                oleSRUExplainServerInfo.setHost(hierarchicalStreamReader.getValue());
056            else if ("zr:port".equals(hierarchicalStreamReader.getNodeName()))
057                oleSRUExplainServerInfo.setPort(hierarchicalStreamReader.getValue());
058            else if ("zr:database".equals(hierarchicalStreamReader.getNodeName()))
059                oleSRUExplainServerInfo.setDatabase(hierarchicalStreamReader.getValue());
060            hierarchicalStreamReader.moveUp();
061        }
062        return oleSRUExplainServerInfo;
063    }
064
065    @Override
066    public boolean canConvert(Class aClass) {
067        return aClass.equals(OleSRUExplainServerInfo.class);
068    }
069}