package org.flymine.objectstore.webservice.ser;

/*
 * Copyright (C) 2002-2003 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.Map;
import javax.xml.namespace.QName;

import org.w3c.dom.Element;
import org.xml.sax.Attributes;

import org.apache.axis.encoding.Serializer;
import org.apache.axis.encoding.SerializationContext;
import org.apache.axis.wsdl.fromJava.Types;

import org.flymine.metadata.Model;
import org.flymine.objectstore.proxy.LazyReference;
import org.flymine.objectstore.query.SingletonResults;
import org.flymine.objectstore.query.fql.FqlQuery;

/**
 * This the serializer (object to xml translator) for all objects used in objectstore calls
 * (except lists...which will eventually be handled by axis)
 *
 * @author Mark Woodbridge
 */
public class DefaultSerializer implements Serializer
{ 
    /**
     * @see Serializer#serialize
     */
    public void serialize(QName name, Attributes attributes, Object value,
                          SerializationContext context) throws IOException {

        context.startElement(name, attributes);

        //model
        if (value instanceof Model) {
            context.writeString(value.toString());
        } else {
            //fqlquery, explainresult, proxybean, businessobject
            Iterator entryIter = SerializationUtil.fieldValues(value).entrySet().iterator();
            while (entryIter.hasNext()) {
                Map.Entry entry = (Map.Entry) entryIter.next();
                Field field = (Field) entry.getKey();
                Object fieldValue = entry.getValue();

                //lazyreference
                if (fieldValue instanceof LazyReference) {
                    LazyReference ref = (LazyReference) fieldValue;
                    if (!ref.isMaterialised()) {
                        fieldValue = new SerializationUtil.ProxyBean(ref.getType().getName(),
                                                                     ref.getFqlQuery(), 
                                                                     ref.getId());
                    }
                }
            
                //singletonresults
                if (fieldValue instanceof SingletonResults) {
                    SingletonResults res = (SingletonResults) fieldValue;
                    fieldValue = new SerializationUtil.ProxyBean(fieldValue.getClass().getName(),
                                                                 new FqlQuery(res.getQuery()),
                                                                 new Integer(-1));
                }

                QName qname = new QName(name.getNamespaceURI(), field.getName());
                QName xmlType = context.getQNameForClass(field.getType());
                context.serialize(qname, null, fieldValue, xmlType, true, null);
            }
        }
        
        context.endElement();
    }

    /**
     * @see Serializer#writeSchema
     */
    public Element writeSchema(Class javaType, Types types) throws Exception {
        return null;
    }

    /**
     * @see Serializer#getMechanismType
     */
    public String getMechanismType() {
        return null;
    }
}

@@ -691,43 +691,8 @@
 s;%0A%0A
-import org.flymine.metadata.Model;%0A
 impo
@@ -846,16 +846,49 @@
 Query;%0A%0A
+import org.apache.log4j.Logger;%0A%0A
 /**%0A * T
@@ -1124,17 +1124,100 @@
 alizer%0A%7B
- 
+%0A    protected static final Logger LOG = Logger.getLogger(DefaultSerializer.class);%0A
 %0A    /**
@@ -1464,134 +1464,8 @@
 );%0A%0A
-        //model%0A        if (value instanceof Model) %7B%0A            context.writeString(value.toString());%0A        %7D else %7B%0A    
     
@@ -1521,20 +1521,16 @@
 sobject%0A
-    
         
@@ -1618,20 +1618,16 @@
         
-   
- 
 while (e
@@ -1644,28 +1644,24 @@
 asNext()) %7B%0A
-    
             
@@ -1720,20 +1720,16 @@
         
-   
- 
 Field fi
@@ -1754,28 +1754,24 @@
 y.getKey();%0A
-    
             
@@ -1821,20 +1821,16 @@
         
-  
-  
 //lazyre
@@ -1837,20 +1837,16 @@
 ference%0A
-    
         
@@ -1908,20 +1908,16 @@
         
-  
-  
 LazyRefe
@@ -1968,28 +1968,24 @@
             
-    
 if (!ref.isM
@@ -2013,36 +2013,32 @@
                 
- 
-   
 fieldValue = new
@@ -2038,34 +2038,16 @@
 e = new 
-SerializationUtil.
 ProxyBea
@@ -2134,28 +2134,24 @@
             
-    
 ref.getFqlQu
@@ -2158,20 +2158,16 @@
 ery(), %0A
-    
         
@@ -2249,34 +2249,26 @@
             
-    %7D%0A    
+%7D%0A
             
@@ -2294,20 +2294,16 @@
         
-    
 //single
@@ -2317,36 +2317,32 @@
 lts%0A            
-   
- 
 if (fieldValue i
@@ -2367,28 +2367,24 @@
 nResults) %7B%0A
-    
             
@@ -2449,36 +2449,32 @@
                 
-    
 fieldValue = new
@@ -2474,34 +2474,16 @@
 e = new 
-SerializationUtil.
 ProxyBea
@@ -2574,28 +2574,24 @@
             
-  
-  
 new FqlQuery
@@ -2665,28 +2665,24 @@
             
-    
 new Integer(
@@ -2699,27 +2699,19 @@
         
-  
-  
 %7D%0A%0A
-    
         
@@ -2788,28 +2788,24 @@
             
- 
-   
 QName xmlTyp
@@ -2847,28 +2847,231 @@
 getType());%0A
+//
     
+         if (field.getType().equals(java.util.List.class)) %7B%0A//                 LOG.warn(%22found a list%22);%0A//                 xmlType = new QName(%22http://soapinterop.org/xsd%22, %22list%22);%0A//             %7D%0A
             
@@ -3135,30 +3135,16 @@
  null);%0A
-            %7D%0A
         

move model (de)serialization to separate classes