Package org.jpox.store.mapped.mapping

Examples of org.jpox.store.mapped.mapping.JavaTypeMapping


                return new NullLiteral(qs);
            }
            else
            {
                DatastoreAdapter dba = qs.getStoreManager().getDatastoreAdapter();                 
                JavaTypeMapping m = dba.getMapping(id.getClass(), qs.getStoreManager(), qs.getClassLoaderResolver());
                return new ObjectLiteral(qs,m,id);
            }
        }
        else if (ObjectExpression.class.isAssignableFrom(expr.getClass()))
        {
View Full Code Here


            for (Iterator it=keys.iterator(); it.hasNext();)
            {
                Object current = it.next();
                if (null != current)
                {
                    JavaTypeMapping m = dba.getMapping(current.getClass(), qs.getStoreManager(), qs.getClassLoaderResolver());
                    ScalarExpression expr = m.newLiteral(qs, current);

                    // Append the SQLExpression (should be a literal) for the
                    // current element.
                    st.append(hadPrev ? "," : "");
                    st.append(expr);
View Full Code Here

            Object value = map.get(((Literal)expr).getValue());
            if( value == null )
            {
                return new NullLiteral(qs);
            }
            JavaTypeMapping m = dba.getMapping(value.getClass(), qs.getStoreManager(), qs.getClassLoaderResolver());
            return new ObjectLiteral(qs,m,value);
        }
        //impossible to invoke Map.get with an expression, only with literals
        //using CASE WHEN booleanexpression THEN expression does not work, since expression can only be
        //one single field (column), however, one could have two CASE expressions
View Full Code Here

            int stmtParamNum = 1;

            while (i.hasNext())
            {
                Object name = i.next();
                JavaTypeMapping mapping = (JavaTypeMapping)parameterMappingsByName.get(name);
                Object value = parameterValuesByName.get(name);

                mapping.setObject(om, datastoreStatement, Mappings.getParametersIndex(stmtParamNum,mapping), value);
                if (mapping.getNumberOfDatastoreFields() > 0)
                {
                    stmtParamNum += mapping.getNumberOfDatastoreFields();
                }
                else
                {
                    //if the JavaTypeMapping has no datastoreFields, at least one it may have
                    stmtParamNum += 1;
View Full Code Here

    public QueryExpression newQueryStatement(Class candidateClass, DatastoreIdentifier candidateAlias)
    {
        QueryExpression stmt = ((Queryable)extent).newQueryStatement(candidateClass, candidateAlias);
        ObjectManager om = extent.getObjectManager();
        MappedStoreManager storeMgr = (MappedStoreManager)om.getStoreManager();
        JavaTypeMapping m = storeMgr.getDatastoreClass(candidateClass.getName(),
            om.getClassLoaderResolver()).getIDMapping();

        /*
         * creates a query like WHERE ... AND (CANDIDATE_ID = ?1 OR CANDIDATE_ID =
         * ?2) or for classes with composite primary keys WHERE ... AND (
         * (CANDIDATE_IDa = ?1a AND CANDIDATE_IDb = ?1b) OR (CANDIDATE_IDa = ?2a
         * AND CANDIDATE_IDb = ?2b) )
         */
        BooleanExpression elementsExpr = null;
        for (Iterator it = userCandidates.iterator(); it.hasNext();)
        {
            Object candidateValue = it.next();
            ScalarExpression expr = m.newScalarExpression(stmt, stmt.getMainTableExpression());
            BooleanExpression keyExpr = expr.eq(m.newLiteral(stmt, candidateValue));
            if (elementsExpr == null)
            {
                elementsExpr = keyExpr;
            }
            else
View Full Code Here

            DatastoreAdapter dba = storeMgr.getDatastoreAdapter();
            ClassLoaderResolver clr = qs.getClassLoaderResolver();
            if (value instanceof OID)
            {
                // Object is an OID
                JavaTypeMapping m = dba.getMapping(((OID)value).getKeyValue().getClass(), storeMgr, clr);
                ScalarExpression oidExpr =  m.newLiteral(qs,((OID)value).getKeyValue());
                bExpr = expr.expressionList.getExpression(0).eq(oidExpr);
            }
            else
            {
                ApiAdapter api = qs.getStoreManager().getApiAdapter();
                AbstractClassMetaData cmd = storeMgr.getOMFContext().getMetaDataManager().getMetaDataForClass(value.getClass(), clr);
                if (cmd == null)
                {
                    // if there is no metadata, we either have an SingleFieldIdentity, application identity, or any object
                    if (storeMgr.getApiAdapter().isSingleFieldIdentityClass(value.getClass().getName()))
                    {
                        // Object is SingleFieldIdentity
                        JavaTypeMapping m = dba.getMapping(api.getTargetClassForSingleFieldIdentity(value), storeMgr, clr);
                        ScalarExpression oidExpr =  m.newLiteral(qs, api.getTargetKeyForSingleFieldIdentity(value));
                        bExpr = expr.expressionList.getExpression(0).eq(oidExpr);
                    }
                    else
                    {
                        String pcClassName = storeMgr.getClassNameForObjectID(value, clr, null);
                        if (pcClassName != null)
                        {
                            // Object is an application identity
                            cmd = storeMgr.getOMFContext().getMetaDataManager().getMetaDataForClass(pcClassName, clr);
                            bExpr = eqApplicationIdentity(value, null, expr, null, storeMgr, clr);
                        }
                        else
                        {
                            // Value not PersistenceCapable nor an identity, so return nothing "(1 = 0)"
                            bExpr = new BooleanLiteral(qs, mapping, false).eq(new BooleanLiteral(qs, mapping, true));
                        }
                    }
                }
                else
                {
                    // Value is a PersistenceCapable
                    if (cmd.getIdentityType() == IdentityType.APPLICATION)
                    {
                        // Application identity
                        if (api.getIdForObject(value) != null)
                        {
                            // Persistent PC object (FCO)
                            // Cater for composite PKs and parts of PK being PC mappings, and recursion
                            JavaTypeMapping[] pkMappingsApp = new JavaTypeMapping[expr.expressionList.size()];
                            Object[] pkFieldValues = new Object[expr.expressionList.size()];
                            int position = 0;
                            for (int i=0;i<cmd.getNoOfPrimaryKeyMembers();i++)
                            {
                                AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(cmd.getPKMemberPositions()[i]);
                                Object fieldValue = getFieldValue(mmd, value);
                                JavaTypeMapping mapping = dba.getMapping(fieldValue.getClass(), storeMgr, clr);
                                if (mapping instanceof PersistenceCapableMapping)
                                {
                                    position = populatePrimaryKeyMappingsValuesForPCMapping(pkMappingsApp,
                                        pkFieldValues, position, (PersistenceCapableMapping)mapping,
                                        cmd, mmd, fieldValue, storeMgr, clr);
                                }
                                else
                                {
                                    pkMappingsApp[position] = mapping;
                                    pkFieldValues[position] = fieldValue;
                                    position++;
                                }
                            }

                            for (int i=0; i<expr.expressionList.size(); i++)
                            {
                                ScalarExpression source = expr.expressionList.getExpression(i);
                                ScalarExpression target = pkMappingsApp[i].newLiteral(qs, pkFieldValues[i]);
                                if (bExpr == null)
                                {
                                    bExpr = source.eq(target);
                                }
                                else
                                {
                                    bExpr = bExpr.and(source.eq(target));
                                }
                            }
                        }
                        else
                        {
                            // PC object with no id (embedded, or transient maybe)
                            for (int i=0; i<expr.expressionList.size(); i++)
                            {
                                // Query should return nothing (so just do "(1 = 0)")
                                JPOXLogger.QUERY.warn(LOCALISER.msg("037003", value));
                                bExpr = new BooleanLiteral(qs, mapping, false).eq(new BooleanLiteral(qs, mapping, true));
                                // It is arguable that we should compare the id with null (as below)
                                /*bExpr = expr.eq(new NullLiteral(qs));*/
                            }
                        }
                    }
                    else
                    {
                        // Datastore identity
                        for (int i=0; i<expr.expressionList.size(); i++)
                        {
                            ScalarExpression source = expr.expressionList.getExpression(i);
                            OID objectId = (OID)api.getIdForObject(value);
                            if (objectId == null)
                            {
                                // PC object with no id (embedded, or transient maybe)
                                // Query should return nothing (so just do "(1 = 0)")
                                JPOXLogger.QUERY.warn(LOCALISER.msg("037003", value));
                                bExpr = new BooleanLiteral(qs, mapping, false).eq(new BooleanLiteral(qs, mapping, true));
                                // It is arguable that we should compare the id with null (as below)
                                /*bExpr = expr.eq(new NullLiteral(qs));*/
                            }
                            else
                            {
                                JavaTypeMapping m = dba.getMapping(objectId.getKeyValue().getClass(), storeMgr, clr);
                                ScalarExpression oidExpr = m.newLiteral(qs, objectId.getKeyValue());
                                bExpr = source.eq(oidExpr);
                            }
                        }
                    }
                }
View Full Code Here

        JavaTypeMapping[] subMappings = pcMapping.getJavaTypeMapping();
        if (subMappings.length == 0)
        {
            // Embedded PC has no PK so must be embedded-only so use mapping from owner table
            DatastoreClass table = storeMgr.getDatastoreClass(cmd.getFullClassName(), clr);
            JavaTypeMapping ownerMapping = table.getFieldMapping(mmd);
            EmbeddedMapping embMapping = (EmbeddedMapping)ownerMapping;
            for (int k=0;k<embMapping.getNumberOfJavaTypeMappings();k++)
            {
                JavaTypeMapping subMapping = embMapping.getJavaTypeMapping(k);
                pkMappings[position] = subMapping;
                pkFieldValues[position] = getFieldValue(subMapping.getFieldMetaData(), fieldValue);
                position++;
            }
        }
        else
        {
View Full Code Here

            }
            else
            {
                //if a simple value, we simply apply the equals
                ScalarExpression source = expr.getExpressionList().getExpression(index.index);
                JavaTypeMapping mapping = storeMgr.getDatastoreAdapter().getMapping(value.getClass(), storeMgr, clr);
                ScalarExpression target = mapping.newLiteral(qs, value);
                if( bExpr == null )
                {
                    bExpr = source.eq(target);
                }
                else
View Full Code Here

            if (fieldValue == null)
            {
                return new NullLiteral(this.qs);
            }
            DatastoreAdapter dba = qs.getStoreManager().getDatastoreAdapter();
            JavaTypeMapping mapping;
            if (mappingClass != null && subfieldName == null)
            {
                mapping = dba.getMapping(qs.getClassLoaderResolver().classForName(mappingClass), qs.getStoreManager(),
                    qs.getClassLoaderResolver());
            }
            else
            {
                mapping = dba.getMapping(fieldValue.getClass(), qs.getStoreManager(), qs.getClassLoaderResolver());               
            }
            return mapping.newLiteral(qs,fieldValue);
        }
        catch (SecurityException e)
        {
            throw new JPOXUserException("Cannot access field: "+subfieldName,e);
        }
View Full Code Here

    {
        if (mainTable instanceof DatastoreClass)
        {
            // Field of class in its primary/secondary table
            DatastoreClass ct = (DatastoreClass) mainTable;
            JavaTypeMapping m = null;
            if (fieldName.equals(qs.getCandidateAlias()))
            {
                // Candidate table so return id mapping
                m = ct.getIDMapping();
                return m.newScalarExpression(qs, this);
            }
            else
            {
                if (fieldName.indexOf(".") > 0)
                {
                    String baseField = fieldName.substring(0, fieldName.indexOf("."));
                    try
                    {
                        m = ct.getFieldMapping(baseField);
                    }
                    catch (NoSuchPersistentFieldException npfe)
                    {
                        // Check if this field is a previously utilised embedded field
                        if (embeddedFieldMappings != null)
                        {
                            m = (JavaTypeMapping)embeddedFieldMappings.get(baseField);
                        }
                        if (m == null)
                        {
                            // Field is not valid for this class, and we have no known embedded mapping for it so its a user error
                            throw npfe;
                        }
                    }
                    if (m == null)
                    {
                        throw new JPOXUserException(LOCALISER.msg("037001", fieldName, ct.toString()));
                    }
                   
                    if (m instanceof EmbeddedPCMapping)
                    {
                        // Embedded PC field
                        String subField = fieldName.substring(fieldName.indexOf(".") + 1);
                        m = getMappingForEmbeddedField((EmbeddedPCMapping)m, subField);
                        if (m == null)
                        {
                            throw new JPOXUserException(LOCALISER.msg("037002", fieldName, subField, baseField));
                        }
                        // Save this embedded mapping in case the user has nested subobjects within it
                        // TODO This doesnt allow for embedded "subfields" having the same name as other embedded subfields
                        // Currently we just keep on saving these against the subfield name, but maybe we only to keep the
                        // most recent since the field will be processed straight away if it's part of a JDOQL query
                        if (embeddedFieldMappings == null)
                        {
                            embeddedFieldMappings = new HashMap();
                        }
                        embeddedFieldMappings.put(subField, m);
                    }
                   
                    ScalarExpression expr = m.newScalarExpression(qs, this);
                    if (expr instanceof ObjectExpression)
                    {
                        ((ObjectExpression)expr).setFieldDefinition(m.getFieldMetaData().getName(), m.getFieldMetaData().getTypeName());
                    }
                    return expr;
                }
                else
                {
                    // Field of main table
                    m = ct.getFieldMapping(fieldName);
                    if (m == null)
                    {
                        throw new JPOXUserException(LOCALISER.msg("037001", fieldName, ct.toString()));
                    }

                    ScalarExpression expr = m.newScalarExpression(qs, this);
                    if (expr instanceof ObjectExpression)
                    {
                        ((ObjectExpression)expr).setFieldDefinition(fieldName, m.getType());
                    }
                    return expr;
                }
            }
        }
        else if (mainTable instanceof DatastoreCollection ||
                 mainTable instanceof DatastoreMap)
        {
            // User has an embedded element/key/value and has a constraint on it
            String fld = fieldName;
            if (fieldName.indexOf(".") > 0)
            {
                // TODO Process the base field - typically is "null". When is it not "null" ?
                String subField = fieldName.substring(fieldName.indexOf(".")+1);
                fld = subField;
            }

            if (mainTable instanceof DatastoreElementContainer)
            {
                // collection/array - element mapping
                DatastoreElementContainer join = (DatastoreElementContainer)mainTable;
                JavaTypeMapping m = join.getElementMapping();
                if (m instanceof EmbeddedElementPCMapping)
                {
                    JavaTypeMapping fieldMapping = ((EmbeddedMapping)m).getJavaTypeMapping(fld);
                    if (fieldMapping != null)
                    {
                        return fieldMapping.newScalarExpression(qs, this);
                    }
                    else
                    {
                        throw new JPOXUserException("'" + fieldName + "' was not found as a field stored in the join table " + mainTable);
                    }
                }
            }
            else if (mainTable instanceof DatastoreMap)
            {
                // Check for a key field first
                DatastoreMap join = (DatastoreMap)mainTable;
                JavaTypeMapping m = join.getKeyMapping();
                if (m instanceof EmbeddedKeyPCMapping)
                {
                    JavaTypeMapping fieldMapping = ((EmbeddedMapping)m).getJavaTypeMapping(fld);
                    if (fieldMapping != null)
                    {
                        return fieldMapping.newScalarExpression(qs, this);
                    }
                }

                // Check for a value field next
                m = join.getValueMapping();
                if (m instanceof EmbeddedValuePCMapping)
                {
                    JavaTypeMapping fieldMapping = ((EmbeddedMapping)m).getJavaTypeMapping(fld);
                    if (fieldMapping != null)
                    {
                        return fieldMapping.newScalarExpression(qs, this);
                    }
                }
            }

            throw new JPOXUserException("'" + fieldName + "' was not found as an embedded element/key/value field stored in the join table " + mainTable);
View Full Code Here

TOP

Related Classes of org.jpox.store.mapped.mapping.JavaTypeMapping

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.