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.Map;
 
FieldManager for inserting data into the provided JSONObject from the ObjectProvider.
 
 public class StoreFieldManager extends AbstractFieldManager
 {
     ObjectProvider op;
     JSONObject jsonobj;
 
     protected boolean insert// Either insert or update
 
     public StoreFieldManager(ObjectProvider opJSONObject jsonobjboolean insert)
     {
         this. = op;
         this. = jsonobj;
         this. = insert;
 
         try
         {
             jsonobj.put("class"op.getClassMetaData().getFullClassName());
         }
         catch (JSONException e)
         {
             throw new NucleusException(e.getMessage(), e);
         }
     }
 
     protected boolean isStorable(int fieldNumber)
     {
         return isStorable(mmd);
     }
 
     protected boolean isStorable(AbstractMemberMetaData mmd)
     {
         if (( && mmd.isInsertable()) || (! && mmd.isUpdateable()))
         {
             return true;
         }
         else
         {
             return false;
         }
     }
 
     public void storeBooleanField(int fieldNumberboolean value)
     {
         if (!isStorable(fieldNumber))
         {
             return;
         }
 
         String name = JSONUtils.getMemberNameForMember(mmd);
         try
         {
             .put(name, (boolean)value);
        }
        catch (JSONException e)
        {
            throw new NucleusException(e.getMessage(), e);
        }
    }
    public void storeByteField(int fieldNumberbyte value)
    {
        if (!isStorable(fieldNumber))
        {
            return;
        }
        String name = JSONUtils.getMemberNameForMember(mmd);
        try
        {
            .put(name, (int)value);
        }
        catch (JSONException e)
        {
            throw new NucleusException(e.getMessage(), e);
        }
    }
    public void storeCharField(int fieldNumberchar value)
    {
        if (!isStorable(fieldNumber))
        {
            return;
        }
        String name = JSONUtils.getMemberNameForMember(mmd);
        try
        {
            .put(namenew Character(value));
        }
        catch (JSONException e)
        {
            throw new NucleusException(e.getMessage(), e);
        }
    }
    public void storeDoubleField(int fieldNumberdouble value)
    {
        if (!isStorable(fieldNumber))
        {
            return;
        }
        String name = JSONUtils.getMemberNameForMember(mmd);
        try
        {
            .put(name, (double)value);
        }
        catch (JSONException e)
        {
            throw new NucleusException(e.getMessage(), e);
        }
    }
    public void storeFloatField(int fieldNumberfloat value)
    {
        if (!isStorable(fieldNumber))
        {
            return;
        }
        String name = JSONUtils.getMemberNameForMember(mmd);
        try
        {
            .put(name, (double)value);
        }
        catch (JSONException e)
        {
            throw new NucleusException(e.getMessage(), e);
        }
    }
    public void storeIntField(int fieldNumberint value)
    {
        if (!isStorable(fieldNumber))
        {
            return;
        }
        String name = JSONUtils.getMemberNameForMember(mmd);
        try
        {
            .put(name, (int)value);
        }
        catch (JSONException e)
        {
            throw new NucleusException(e.getMessage(), e);
        }
    }
    public void storeLongField(int fieldNumberlong value)
    {
        if (!isStorable(fieldNumber))
        {
            return;
        }
        String name = JSONUtils.getMemberNameForMember(mmd);
        try
        {
            .put(name, (long)value);
        }
        catch (JSONException e)
        {
            throw new NucleusException(e.getMessage(), e);
        }
    }
    public void storeShortField(int fieldNumbershort value)
    {
        if (!isStorable(fieldNumber))
        {
            return;
        }
        String name = JSONUtils.getMemberNameForMember(mmd);
        try
        {
            .put(name, (int)value);
        }
        catch (JSONException e)
        {
            throw new NucleusException(e.getMessage(), e);
        }
    }
    public void storeStringField(int fieldNumberString value)
    {
        if (!isStorable(fieldNumber))
        {
            return;
        }
        String name = JSONUtils.getMemberNameForMember(mmd);
        try
        {
            if (value == null)
            {
                .put(name.);
            }
            else
            {
                .put(namevalue);
            }
        }
        catch (JSONException e)
        {
            throw new NucleusException(e.getMessage(), e);
        }
    }
    public void storeObjectField(int fieldNumberObject value)
    {
        if (!isStorable(mmd))
        {
            return;
        }
        ExecutionContext ec = .getExecutionContext();
        ClassLoaderResolver clr = ec.getClassLoaderResolver();
        int relationType = mmd.getRelationType(clr);
        if (Relation.isRelationSingleValued(relationType) && mmd.isEmbedded())
        {
            // Persistable object embedded into this table TODO Support this
            throw new NucleusException("Embedded fields are not supported");
        }
        try
        {
            storeObjectFieldInternal(fieldNumbervaluemmdclr);
        }
        catch (JSONException e)
        {
            throw new NucleusException(e.getMessage(), e);
        }
    }
    protected void storeObjectFieldInternal(int fieldNumberObject valueAbstractMemberMetaData mmdClassLoaderResolver clr)
    throws JSONException
    {
        int relationType = mmd.getRelationType(clr);
        String name = JSONUtils.getMemberNameForMember(mmd);
        if (value == null)
        {
            .put(name.);
            return;
        }
        else if (relationType == .)
        {
            if (value instanceof Boolean)
            {
                .put(name, ((Boolean)value).booleanValue());
            }
            else if (value instanceof Integer)
            {
                .put(name, ((Integer)value).intValue());
            }
            else if (value instanceof Long)
            {
                .put(name, ((Long)value).longValue());
            }
            else if (value instanceof Double)
            {
                .put(name, ((Double)value).doubleValue());
            }
            else if (value instanceof Enum)
            {
                ColumnMetaData[] colmds = mmd.getColumnMetaData();
                boolean persistAsString = true;
                if (colmds != null && colmds.length == 1 && colmds[0].getJdbcType() != null)
                {
                    String jdbcType = colmds[0].getJdbcType();
                    if (jdbcType.equalsIgnoreCase("INTEGER") || jdbcType.equalsIgnoreCase("NUMERIC"))
                    {
                        persistAsString = false;
                    }
                }
                if (persistAsString)
                {
                    .put(name, ((Enum)value).name());
                }
                else
                {
                    .put(name, ((Enum)value).ordinal());
                }
            }
            else if (value instanceof BigDecimal)
            {
                .put(namevalue);
            }
            else if (value instanceof BigInteger)
            {
                .put(namevalue);
            }
            else if (value instanceof Collection)
            {
                // Collection<Non-PC> will be returned as JSONArray
                .put(namevalue);
            }
            else if (value instanceof Map)
            {
                .put(namevalue);
            }
            else
            {
                // See if we can persist it as a Long/String
                boolean useLong = false;
                ColumnMetaData[] colmds = mmd.getColumnMetaData();
                if (colmds != null && colmds.length == 1)
                {
                    String jdbc = colmds[0].getJdbcType();
                    if (jdbc != null && (jdbc.equalsIgnoreCase("INTEGER") || jdbc.equalsIgnoreCase("NUMERIC")))
                    {
                        useLong = true;
                    }
                }
                ObjectStringConverter strConv = 
                    .getExecutionContext().getNucleusContext().getTypeManager().getStringConverter(value.getClass());
                ObjectLongConverter longConv = 
                    .getExecutionContext().getNucleusContext().getTypeManager().getLongConverter(value.getClass());
                if (useLong)
                {
                    if (longConv != null)
                    {
                        .put(namelongConv.toLong(value));
                    }
                }
                else
                {
                    if (strConv != null)
                    {
                        .put(namestrConv.toString(value));
                    }
                    else if (longConv != null)
                    {
                        .put(namelongConv.toLong(value));
                    }
                }
                // Just persist as a JSONObject and see what happens
                JSONObject jsonobjfield = new JSONObject(value);
                jsonobjfield.put("class"value.getClass().getName());
                .put(namejsonobjfield);
            }
            return;
        }
        else if (Relation.isRelationSingleValued(relationType))
        {
            // 1-1, N-1 relation, so store the "id"
            Object valuePC = .getExecutionContext().persistObjectInternal(valuefieldNumber, -1);
            Object valueId = .getExecutionContext().getApiAdapter().getIdForObject(valuePC);
            .put(namevalueId);
            return;
        }
        else if (Relation.isRelationMultiValued(relationType))
        {
            // Collection/Map/Array
            if (mmd.hasCollection())
            {
                Collection idColl = new ArrayList();
                Collection coll = (Collection)value;
                Iterator collIter = coll.iterator();
                while (collIter.hasNext())
                {
                    Object element = collIter.next();
                    Object elementPC = .getExecutionContext().persistObjectInternal(elementfieldNumber, -1);
                    Object elementID = .getExecutionContext().getApiAdapter().getIdForObject(elementPC);
                    idColl.add(elementID.toString());
                }
                .put(nameidColl);
                return;
            }
            else if (mmd.hasArray())
            {
                Collection ids = new ArrayList(Array.getLength(value));
                for (int i=0;i<Array.getLength(value);i++)
                {
                    Object element = Array.get(valuei);
                    Object elementPC = .getExecutionContext().persistObjectInternal(elementfieldNumber, -1);
                    Object elementID = .getExecutionContext().getApiAdapter().getIdForObject(elementPC);
                    ids.add(elementID.toString());
                }
                .put(nameids);
                return;
            }
            else if (mmd.hasMap())
            {
                AbstractClassMetaData keyCmd = mmd.getMap().getKeyClassMetaData(clr.getExecutionContext().getMetaDataManager());
                AbstractClassMetaData valCmd = mmd.getMap().getValueClassMetaData(clr.getExecutionContext().getMetaDataManager());
                Map idMap = new HashMap();
                Map map = (Map)value;
                Iterator<Map.EntrymapIter = map.entrySet().iterator();
                while (mapIter.hasNext())
                {
                    Map.Entry entry = mapIter.next();
                    Object key = null;
                    Object val = null;
                    if (keyCmd != null)
                    {
                        Object keyPC = .getExecutionContext().persistObjectInternal(entry.getKey(), fieldNumber, -1);
                        key = .getExecutionContext().getApiAdapter().getIdForObject(keyPC);
                    }
                    else
                    {
                        key = entry.getKey();
                    }
                    if (valCmd != null)
                    {
                        Object valPC = .getExecutionContext().persistObjectInternal(entry.getValue(), fieldNumber, -1);
                        val = .getExecutionContext().getApiAdapter().getIdForObject(valPC);
                    }
                    else
                    {
                        val = entry.getValue();
                    }
                    idMap.put(keyval);
                }
                .put(nameidMap);
                return;
            }
        }
        throw new NucleusException("Dont currently support field " + mmd.getFullFieldName() + " of type " + mmd.getTypeName());
    }
New to GrepCode? Check out our FAQ X