package com.sharefile.api.gson.auto;

import java.lang.reflect.Type;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.sharefile.api.gson.SFGsonHelper;
import com.sharefile.api.models.SFItem;
import com.sharefile.api.models.SFODataObject;

/**
 *   This class read the odata.metadata from the JsonElement to find out the real type of object contained inside the object 
 *   and the routes the parsing back to the correct defuault gson parser.
 */
public class SFGsonRouter implements JsonDeserializer<SFODataObject>, JsonSerializer<SFODataObject>
{		
	@Override
	public SFODataObject deserialize(JsonElement jsonElement, Type typeOfObject,JsonDeserializationContext desContext) throws JsonParseException 
	{		
		return SFGsonHelper.customParse(jsonElement);
	}

	@Override
	public JsonElement serialize(SFODataObject sfODataObject, Type typeOfObject,JsonSerializationContext serContext) 
	{	
		JsonElement ret = null;
		String str = null;
		
		if(sfODataObject instanceof SFItem) {
			str = "{\"Id\":\"" + sfODataObject.getId() + "\"}";
		}
		else {
			str = SFDefaultGsonParser.serialize(typeOfObject, sfODataObject);
		}
		
		if(str!=null)
		{
			JsonParser parser = new JsonParser();
			ret = parser.parse(str);
		}	
		
		/*
		SFV3ElementType elementType = SFModelFactory.getElementTypeClassName(typeOfObject.getClass().getName());
		
		switch(elementType)
		{
			case AccountUser:				
				str = SFDefaultGsonParser.serialize(SFAccountUser.class, sfODataObject);				
			break;
			
			case File:
				str = SFDefaultGsonParser.serialize(SFFile.class, sfODataObject);
			break;
			
			case Folder:
				str = SFDefaultGsonParser.serialize(SFFolder.class, sfODataObject);
			break;
			
			case Link:
				str = SFDefaultGsonParser.serialize(SFLink.class, sfODataObject);
			break;
			
			case Note:
				str = SFDefaultGsonParser.serialize(SFNote.class, sfODataObject);
			break;
			
			default:
				SFToDoReminderException.throwTODOException("Need to implement parser for : " + elementType.toString());
			break;	
		}*/
		
		return ret;
	}			
}
@@ -487,16 +487,92 @@
 aObject;
+%0Aimport com.sharefile.api.models.SFUser;%0Aimport com.sharefile.java.log.SLog;
 %0A%0A/**%0A *
@@ -877,16 +877,67 @@
 ct%3E%0A%7B%09%09%0A
+%09private static final String TAG = %22SFGsonRouter%22;%0A
 %09@Overri
@@ -1359,16 +1359,16 @@
 Item) %7B%0A
-
 %09%09%09str =
@@ -1418,16 +1418,817 @@
 %7D%22;%0A%09%09%7D%0A
+%09%09else if(sfODataObject instanceof SFUser) %7B%0A%09%09%09SFUser sfuser = (SFUser) sfODataObject;%0A%09%09%09str = %22%7B%22;%0A%09%09%09if(sfuser.getId()!=null && sfuser.getId().length()%3E0)%0A%09%09%09%09str += %22%5C%22Id%5C%22:%5C%22%22 + sfuser.getId() + %22%5C%22,%22;%0A%09%09%09if(sfuser.getEmail()!=null && sfuser.getEmail().length()%3E0)%0A%09%09%09%09str += %22%5C%22Email%5C%22:%5C%22%22 + sfuser.getEmail() + %22%5C%22,%22;%0A%09%09%09if(sfuser.getFirstName()!=null && sfuser.getFirstName().length()%3E0)%0A%09%09%09%09str += %22%5C%22FirstName%5C%22:%5C%22%22 + sfuser.getFirstName() + %22%5C%22,%22;%0A%09%09%09if(sfuser.getLastName()!=null && sfuser.getLastName().length()%3E0)%0A%09%09%09%09str += %22%5C%22LastName%5C%22:%5C%22%22 + sfuser.getLastName() + %22%5C%22,%22;%0A%09%09%09if(sfuser.getCompany()!=null && sfuser.getCompany().length()%3E0)%0A%09%09%09%09str += %22%5C%22Company%5C%22:%5C%22%22 + sfuser.getCompany() + %22%5C%22,%22;%0A%09%09%09if(str.length()%3E2)%0A%09%09%09%09str = str.substring(0, str.length()-1);%0A%09%09%09str += %22%7D%22;%0A%09%09%7D%0A
 %09%09else %7B

Fix the issue where the app will crash when we include "SFPincipal" objects in the query request.