Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2008 Erik Bengtson and others. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Contributors: ... /
  
  package org.datanucleus.store.json.fieldmanager;
  
  import java.util.List;
  
  
  import  org.datanucleus.OMFContext;
  import  org.datanucleus.ObjectManager;
  import  org.datanucleus.StateManager;
FieldManager for fetching from JSON.
  
  public class FetchFieldManager implements FieldManager
  {
      StateManager sm;
      JSONObject result;
  
      public FetchFieldManager(StateManager smJSONObject result)
      {
          this. = sm;
          this. = result;
      }
  
      public String fetchStringField(int fieldNumber)
      {
          String fieldName = .getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber).getName();
          if (.isNull(fieldName))
          {
              return null;
          }
          try
          {
              return .getString(fieldName);
          }
          catch (JSONException e)
          {
              //ignore
              return null;
          }
      }
  
      public short fetchShortField(int fieldNumber)
      {
          String fieldName = .getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber).getName();
          if.isNull(fieldName) )
          {
              return 0;
          }
          try
          {
              return (short.getInt(fieldName);
          }
          catch (JSONException e)
          {
              //ignore
              return 0;
          }
      }
  
     public Object fetchObjectField(int fieldNumber)
     {
         AbstractMemberMetaData mmd = .getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber);
         String fieldName = .getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber).getName();
         if.isNull(fieldName) )
         {
             return null;
         }
         try
         {
             Object value = .get(fieldName);
             if (value instanceof JSONObject)
             {
                 return getObjectFromJSONObject((JSONObjectvaluemmd.getType().getName(), .getObjectManager());
             }
             else if (value instanceof JSONArray)
             {
                 return fetchJSONArray((JSONArray)valuefieldNumber.getObjectManager(), .getClassMetaData());
             }
             else 
             {
                 return TypeConversionHelper.convertTo(.get(fieldName), mmd.getType());
             }
         }
         catch (JSONException e)
         {
             throw new NucleusException(e.getMessage(), e);
         }
         
     }
     private List fetchJSONArray(JSONArray array,  int position, ObjectManager omAbstractClassMetaData cmdthrows JSONException
     {
         List elements = new ArrayList();
         forint i=0; i<array.length(); i++)
         {
             ifarray.isNull(i) )
             {
                 elements.add(null);
             }
             else
             {
                 Object value = array.get(i);
                 
                 ifvalue instanceof JSONObject)
                 {
                     elements.add(getObjectFromJSONObject((JSONObject)value, ((JSONObject)value).getString("class"), om));
                 }
                 else ifvalue instanceof JSONArray)
                 {
                     elements.add(fetchJSONArray((JSONArray)value,positionomcmd));
                 }
                 else
                 {
                     elements.add(TypeConversionHelper.convertTo(valuecmd.getMetaDataForManagedMemberAtAbsolutePosition(position).getType()));
                 }
             }
         }
         return elements;
     }
     
     public long fetchLongField(int fieldNumber)
     {
         String fieldName = .getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber).getName();
         if.isNull(fieldName) )
         {
             return 0;
         }
         try
         {
             return .getLong(fieldName);
         }
         catch (JSONException e)
         {
             //ignore
             return 0;
         }
     }
 
     public int fetchIntField(int fieldNumber)
     {
         String fieldName = .getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber).getName();
         if.isNull(fieldName) )
         {
             return 0;
         }
         try
         {
             return .getInt(fieldName);
         }
         catch (JSONException e)
         {
             //ignore
             return 0;
         }
     }
 
     public float fetchFloatField(int fieldNumber)
     {
         String fieldName = .getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber).getName();
         if.isNull(fieldName) )
         {
             return 0;
         }
         try
         {
             return (float.getDouble(fieldName);
         }
         catch (JSONException e)
         {
             //ignore
             return 0;
         }
     }
 
     public double fetchDoubleField(int fieldNumber)
     {
         String fieldName = .getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber).getName();
         if.isNull(fieldName) )
         {
             return 0;
         }
         try
         {
             return .getDouble(fieldName);
         }
         catch (JSONException e)
         {
             //ignore
             return 0;
         }
     }
 
     public char fetchCharField(int fieldNumber)
     {
         String fieldName = .getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber).getName();
         if.isNull(fieldName) )
         {
             return 0;
         }
         try
         {
             return .getString(fieldName).charAt(0);
         }
         catch (JSONException e)
         {
             //ignore
             return 0;
         }
     }
 
     public byte fetchByteField(int fieldNumber)
     {
         String fieldName = .getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber).getName();
         if.isNull(fieldName) )
         {
             return 0;
         }
         try
         {
             String str = .getString(fieldName);
             return new Byte(str).byteValue();
         }
         catch (JSONException e)
         {
             //ignore
             return 0;
         }
     }
 
     public boolean fetchBooleanField(int fieldNumber)
     {
         String fieldName = .getClassMetaData().getMetaDataForManagedMemberAtAbsolutePosition(fieldNumber).getName();
         if.isNull(fieldName) )
         {
             return false;
         }
         try
         {
             return .getBoolean(fieldName);
         }
         catch (JSONException e)
         {
             //ignore
             return false;
         }
     }
 
     public void storeStringField(int fieldNumberString value)
     {
         // TODO Auto-generated method stub
     }
 
     public void storeShortField(int fieldNumbershort value)
     {
         // TODO Auto-generated method stub
     }
 
     public void storeObjectField(int fieldNumberObject value)
     {
         // TODO Auto-generated method stub
     }
 
     public void storeLongField(int fieldNumberlong value)
     {
         // TODO Auto-generated method stub
     }
 
     public void storeIntField(int fieldNumberint value)
     {
         // TODO Auto-generated method stub
     }
 
     public void storeFloatField(int fieldNumberfloat value)
     {
         // TODO Auto-generated method stub
     }
 
     public void storeDoubleField(int fieldNumberdouble value)
     {
         // TODO Auto-generated method stub
     }
 
     public void storeCharField(int fieldNumberchar value)
     {
         // TODO Auto-generated method stub
     }
 
     public void storeByteField(int fieldNumberbyte value)
     {
         // TODO Auto-generated method stub
     }
 
     public void storeBooleanField(int fieldNumberboolean value)
     {
         // TODO Auto-generated method stub
     }
    
    

Parameters:
jsonobj JSON Object
className The class name
om The object manager
Returns:
The object for the supplied JSON object
Throws:
ClassNotResolvedException when the class is not found
JDOFatalUserException many situations, such as for google appengine when trying the set primary key of a PC_NEW
 
     protected Object getObjectFromJSONObject(final JSONObject jsonobjString className, ObjectManager om)
     {
         OMFContext ctx = om.getOMFContext();
 
         ClassLoaderResolver clr = om.getClassLoaderResolver();
         Class cls = clr.classForName(classNametrue);
         AbstractClassMetaData cmd = ctx.getMetaDataManager().getMetaDataForClass(clsclr);
 
         if (cmd == null)
         {
             return getObject(clrjsonobjcls);
         }
         int[] fieldNumbers = cmd.getAllMemberPositions();
         LocalSM sm1 = null;
         sm1 = new LocalSM(cls);
         sm1.replaceFields(fieldNumbersnew LocalFieldManager(jsonobjnullcmdom));
         Object obj = sm1.getObject();
         try
         {
             Object id = om.getApiAdapter().getNewApplicationIdentityObjectId(sm1.getObject(), cmd);
             obj = om.findObject(idfalsetruenull);
 
             StateManager sm = om.findStateManager(obj);
             fieldNumbers = cmd.getNonPKMemberPositions();
             sm.replaceFields(fieldNumbersnew LocalFieldManager(jsonobjsmcmdom));
             return obj;
         }
         catch (JDONullIdentityException ex)
         {
             // raised when the object has no identity values, so we assume this is a new object
             sm1.disconnect();
             return obj;
         }
         catch (JDOObjectNotFoundException ex)
         {
             // raised when the object cannot be found, so we assume this is a new object
             sm1.disconnect();
             return obj;
         }
 
     }
  
     class LocalFieldManager implements FieldManager
     {
         JSONObject jsonobj;
 
         AbstractClassMetaData cmd;
 
         StateManager sm;
 
         ObjectManager pm;

        

Parameters:
jsonobj
sm may be null
cmd
pm
 
         LocalFieldManager(JSONObject jsonobj, StateManager smAbstractClassMetaData cmd, ObjectManager pm)
         {
             this. = jsonobj;
             this. = cmd;
             this. = sm;
             this. = pm;
         }
 
         public String fetchStringField(int position)
         {
             String fieldName = .getMetaDataForManagedMemberAtAbsolutePosition(position).getName();
             if (!.has(fieldName))
             {
                 return null;
             }
             try
             {
                 String value = .getString(fieldName);
                 if ( != null)
                 {
                     .makeDirty(position);
                 }
                 return value;
             }
             catch (JSONException e)
             {
                 // should not happen
             }
             return null;
         }
 
         public short fetchShortField(int position)
         {
             String fieldName = .getMetaDataForManagedMemberAtAbsolutePosition(position).getName();
             if (!.has(fieldName))
             {
                 return 0;
             }
             try
             {
                 short value = (short.getInt(fieldName);
                 if ( != null)
                 {
                     .makeDirty(position);
                 }
                 return value;
             }
             catch (JSONException e)
             {
                 // should not happen
             }
             return 0;
         }
 
         public Object fetchObjectField(int position)
         {
             String fieldName = .getMetaDataForManagedMemberAtAbsolutePosition(position).getName();
             if (!.has(fieldName))
             {
                 return null;
             }
             try
             {
                     if (.isNull(fieldName))
                     {
                         return null;
                     }
                     Object value = .get(fieldName);
                     ifvalue instanceof JSONObject)
                     {
                         value = getObjectFromJSONObject((JSONObject)value, ((JSONObject)value).getString("class"), );
                         if ( != null)
                         {
                             .makeDirty(position);
                         }
                         return value;
                     }
                     ifvalue instanceof JSONArray)
                     {
                         valuefetchJSONArray((JSONArray)value,position);
                         if ( != null)
                         {
                             .makeDirty(position);
                         }
                         return value;
                     }
                     if ( != null)
                     {
                         .makeDirty(position);
                     }
                     return TypeConversionHelper.convertTo(value.getMetaDataForManagedMemberAtAbsolutePosition(position).getType());
             }
             catch(JSONException ex)
             {
                 throw new RuntimeException(ex);
             }
         }
 
         private List fetchJSONArray(JSONArray array,  int positionthrows JSONException
         {
             List elements = new ArrayList();
             forint i=0; i<array.length(); i++)
             {
                 ifarray.isNull(i) )
                 {
                     elements.add(null);
                 }
                 else
                 {
                     Object value = array.get(i);
                     
                     ifvalue instanceof JSONObject)
                     {
                         elements.add(getObjectFromJSONObject((JSONObject)value, ((JSONObject)value).getString("class"), ));
                     }
                     else ifvalue instanceof JSONArray)
                     {
                         elements.add(fetchJSONArray((JSONArray)value,position));
                     }
                     else
                     {
                         elements.add(TypeConversionHelper.convertTo(value.getMetaDataForManagedMemberAtAbsolutePosition(position).getType()));
                     }
                 }
             }
             return elements;
         }
         
         public long fetchLongField(int position)
         {
             String fieldName = .getMetaDataForManagedMemberAtAbsolutePosition(position).getName();
             if (!.has(fieldName))
             {
                 return 0;
             }
             try
             {
                 long value = .getLong(fieldName);
                 if ( != null)
                 {
                     .makeDirty(position);
                 }
                 return value;
             }
             catch (JSONException e)
             {
                 // should not happen
             }
             return 0;
         }
 
         public int fetchIntField(int position)
         {
             String fieldName = .getMetaDataForManagedMemberAtAbsolutePosition(position).getName();
             if (!.has(fieldName))
             {
                 return 0;
             }
             try
             {
                 int value = .getInt(fieldName);
                 if ( != null)
                 {
                     .makeDirty(position);
                 }
                 return value;
             }
             catch (JSONException e)
             {
                 // should not happen
             }
             return 0;
         }
 
         public float fetchFloatField(int position)
         {
             String fieldName = .getMetaDataForManagedMemberAtAbsolutePosition(position).getName();
             if (!.has(fieldName))
             {
                 return 0;
             }
             try
             {
                 float value = (float.getDouble(fieldName);
                 if ( != null)
                 {
                     .makeDirty(position);
                 }
                 return value;
             }
             catch (JSONException e)
             {
                 // should not happen
             }
             return 0;
         }
 
         public double fetchDoubleField(int position)
         {
             String fieldName = .getMetaDataForManagedMemberAtAbsolutePosition(position).getName();
             if (!.has(fieldName))
             {
                 return 0;
             }
             try
             {
                 double value = .getDouble(fieldName);
                 if ( != null)
                 {
                     .makeDirty(position);
                 }
                 return value;
             }
             catch (JSONException e)
             {
                 // should not happen
             }
             return 0;
         }
 
         public char fetchCharField(int position)
         {
             String fieldName = .getMetaDataForManagedMemberAtAbsolutePosition(position).getName();
             if (!.has(fieldName))
             {
                 return 0;
             }
             try
             {
                 String str = .getString(fieldName);
                 char value = 0;
                 if (str != null && str.length() > 0)
                 {
                     value = str.charAt(0);
                 }
                 if ( != null)
                 {
                     .makeDirty(position);
                 }
                 return value;
             }
             catch (JSONException e)
             {
                 // should not happen
             }
             return 0;
         }
 
         public byte fetchByteField(int position)
         {
             String fieldName = .getMetaDataForManagedMemberAtAbsolutePosition(position).getName();
             if (!.has(fieldName))
             {
                 return 0;
             }
             try
             {
                 String str = .getString(fieldName);
                 byte value = 0;
                 if (str != null && str.length() > 0)
                 {
                     value = str.getBytes()[0];
                 }
                 if ( != null)
                 {
                     .makeDirty(position);
                 }
                 return value;
             }
             catch (JSONException e)
             {
                 // should not happen
             }
             return 0;
         }
 
         public boolean fetchBooleanField(int position)
         {
             String fieldName = .getMetaDataForManagedMemberAtAbsolutePosition(position).getName();
             if (!.has(fieldName))
             {
                 return false;
             }
             try
             {
                 boolean value = .getBoolean(fieldName);
                 if ( != null)
                 {
                     .makeDirty(position);
                 }
                 return value;
             }
             catch (JSONException e)
             {
                 // should not happen
             }
             return false;
         }
 
         public void storeStringField(int arg0String arg1)
         {
         }
 
         public void storeShortField(int arg0short arg1)
         {
         }
 
         public void storeObjectField(int arg0Object arg1)
         {
         }
 
         public void storeLongField(int arg0long arg1)
         {
         }
 
         public void storeIntField(int arg0int arg1)
         {
         }
 
         public void storeFloatField(int arg0float arg1)
         {
         }
 
         public void storeDoubleField(int arg0double arg1)
         {
         }
 
         public void storeCharField(int arg0char arg1)
         {
         }
 
         public void storeByteField(int arg0byte arg1)
         {
         }
 
         public void storeBooleanField(int arg0boolean arg1)
         {
         }
     }

    
This class is used to populate fields of PC objects
 
     private class LocalSM implements javax.jdo.spi.StateManager
     {
         PersistenceCapable myPC;
 
         FieldManager fm;
 
         public LocalSM(Class cls)
         {
              = JDOImplHelper.getInstance().newInstance(clsthis);
         }
 
         public boolean getBooleanField(PersistenceCapable arg0int arg1boolean arg2)
         {
             return false;
         }
 
         public byte getByteField(PersistenceCapable arg0int arg1byte arg2)
         {
             return 0;
         }
 
         public char getCharField(PersistenceCapable arg0int arg1char arg2)
         {
             return 0;
         }
 
         public double getDoubleField(PersistenceCapable arg0int arg1double arg2)
         {
             return 0;
         }
 
         public float getFloatField(PersistenceCapable arg0int arg1float arg2)
         {
             return 0;
         }
 
         public int getIntField(PersistenceCapable arg0int arg1int arg2)
         {
             return 0;
         }
 
         public long getLongField(PersistenceCapable arg0int arg1long arg2)
         {
             return 0;
         }
 
         public Object getObjectField(PersistenceCapable arg0int arg1Object arg2)
         {
             return null;
         }
 
         public Object getObjectId(PersistenceCapable arg0)
         {
             return null;
         }
 
         {
             return null;
         }
 
         public short getShortField(PersistenceCapable arg0int arg1short arg2)
         {
             return 0;
         }
 
         public String getStringField(PersistenceCapable arg0int arg1String arg2)
         {
             return null;
         }
 
         public Object getTransactionalObjectId(PersistenceCapable arg0)
         {
             return null;
         }
 
         public Object getVersion(PersistenceCapable arg0)
         {
             return null;
         }
 
         public boolean isDeleted(PersistenceCapable arg0)
         {
             return false;
         }
 
         public boolean isDirty(PersistenceCapable arg0)
         {
             return false;
         }
 
         public boolean isLoaded(PersistenceCapable arg0int arg1)
         {
             return false;
         }
 
         public boolean isNew(PersistenceCapable arg0)
         {
             return false;
         }
 
         public boolean isPersistent(PersistenceCapable arg0)
         {
             return false;
         }
 
         public boolean isTransactional(PersistenceCapable arg0)
         {
             return false;
         }
 
         public void makeDirty(PersistenceCapable arg0String arg1)
         {
         }
 
         public void preSerialize(PersistenceCapable arg0)
         {
         }
 
         public void providedBooleanField(PersistenceCapable arg0int arg1boolean arg2)
         {
         }
 
         public void providedByteField(PersistenceCapable arg0int arg1byte arg2)
         {
         }
 
         public void providedCharField(PersistenceCapable arg0int arg1char arg2)
         {
         }
 
         public void providedDoubleField(PersistenceCapable arg0int arg1double arg2)
         {
         }
 
         public void providedFloatField(PersistenceCapable arg0int arg1float arg2)
         {
         }
 
         public void providedIntField(PersistenceCapable arg0int arg1int arg2)
         {
         }
 
         public void providedLongField(PersistenceCapable arg0int arg1long arg2)
         {
         }
 
         public void providedObjectField(PersistenceCapable arg0int arg1Object arg2)
         {
         }
 
         public void providedShortField(PersistenceCapable arg0int arg1short arg2)
         {
         }
 
         public void providedStringField(PersistenceCapable arg0int arg1String arg2)
         {
         }
 
         public boolean replacingBooleanField(PersistenceCapable arg0int arg1)
         {
             return .fetchBooleanField(arg1);
         }
 
         public byte replacingByteField(PersistenceCapable arg0int arg1)
         {
             return .fetchByteField(arg1);
         }
 
         public char replacingCharField(PersistenceCapable arg0int arg1)
         {
             return .fetchCharField(arg1);
         }
 
         public Object[] replacingDetachedState(Detachable arg0Object[] arg1)
         {
             return null;
         }
 
         public double replacingDoubleField(PersistenceCapable arg0int arg1)
         {
             return .fetchDoubleField(arg1);
         }
 
         public byte replacingFlags(PersistenceCapable arg0)
         {
             return 0;
         }
 
         public float replacingFloatField(PersistenceCapable arg0int arg1)
         {
             return .fetchFloatField(arg1);
         }
 
         public int replacingIntField(PersistenceCapable arg0int arg1)
         {
             return .fetchIntField(arg1);
         }
 
         public long replacingLongField(PersistenceCapable arg0int arg1)
         {
             return .fetchLongField(arg1);
         }
 
         public Object replacingObjectField(PersistenceCapable arg0int arg1)
         {
             return .fetchObjectField(arg1);
         }
 
         public short replacingShortField(PersistenceCapable arg0int arg1)
         {
             return .fetchShortField(arg1);
         }
 
         {
             return null;
         }
 
         public String replacingStringField(PersistenceCapable arg0int arg1)
         {
             return .fetchStringField(arg1);
         }
 
         public void setBooleanField(PersistenceCapable arg0int arg1boolean arg2boolean arg3)
         {
         }
 
         public void setByteField(PersistenceCapable arg0int arg1byte arg2byte arg3)
         {
         }
 
         public void setCharField(PersistenceCapable arg0int arg1char arg2char arg3)
         {
         }
 
         public void setDoubleField(PersistenceCapable arg0int arg1double arg2double arg3)
         {
         }
 
         public void setFloatField(PersistenceCapable arg0int arg1float arg2float arg3)
         {
         }
 
         public void setIntField(PersistenceCapable arg0int arg1int arg2int arg3)
         {
         }
 
         public void setLongField(PersistenceCapable arg0int arg1long arg2long arg3)
         {
         }
 
         public void setObjectField(PersistenceCapable arg0int arg1Object arg2Object arg3)
        {
        }
        public void setShortField(PersistenceCapable arg0int arg1short arg2short arg3)
        {
        }
        public void setStringField(PersistenceCapable arg0int arg1String arg2String arg3)
        {
        }
        void replaceFields(int[] fieldNumbersFieldManager fm)
        {
            this. = fm;
            .jdoReplaceFields(fieldNumbers);
        }
        public Object getObject()
        {
            return ;
        }
        public void disconnect()
        {
            try
            {
                // Calls to pc.jdoReplaceStateManager must be run privileged
                AccessController.doPrivileged(new PrivilegedAction()
                {
                    public Object run()
                    {
                        .jdoReplaceStateManager(null);
                        return null;
                    }
                });
            }
            catch (SecurityException e)
            {
            }
        }
    }
 
    
Deserialise from JSON to an object. (Used for non PersistenceCapable classes)

Parameters:
jsonobj
cls
Returns:
    private Object getObject(final ClassLoaderResolver clrfinal JSONObject jsonobjfinal Class cls)
    {
        if (cls.getName().equals("com.google.appengine.api.users.User"))
        {
            String email = null;
            String authDomain = null;
            try
            {
                email = jsonobj.getString("email");
            }
            catch (JSONException e)
            {
                // should not happen if the field exists
            }
            try
            {
                authDomain = jsonobj.getString("authDomain");
            }
            catch (JSONException e)
            {
                // should not happen if the field exists
            }
            return ClassUtils.newInstance(clsnew Class[]{String.classString.class}, new String[]{emailauthDomain});
        }
        else if (cls.getName().equals("com.google.appengine.api.datastore.Key"))
        {
            try
            {
                Object parent = null;
                if (jsonobj.has("parent") && !jsonobj.isNull("parent"))
                {
                    
                    //if it's a JSONObject
                    JSONObject parentobj = jsonobj.getJSONObject("parent");
                    parent = getObject(clrparentobj,clr.classForName(jsonobj.getString("class")));
                }
                if (jsonobj.has("appId"))
                {
                    String appId = jsonobj.getString("appId");
                    String kind = jsonobj.getString("kind");
                    Class keyFactory = Class.forName("com.google.appengine.api.datastore.KeyFactory",false,cls.getClassLoader());
                    ifparent != null)
                    {
                        return ClassUtils.getMethodForClass(keyFactory"createKey"new Class[]{cls,String.class,String.class}).invoke(nullnew Object[]{parent,kind,appId});
                    }
                    else
                    {
                        return ClassUtils.getMethodForClass(keyFactory"createKey"new Class[]{String.class,String.class}).