Search in sources :

Example 76 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class ChildDiffLoader method nodeCreated.

public void nodeCreated(Object nodeId) {
    setExternalChange(Boolean.TRUE);
    try {
        ObjectId id = (ObjectId) nodeId;
        if (id.getEntityName() == null) {
            throw new NullPointerException("Null entity name in id " + id);
        }
        ObjEntity entity = context.getEntityResolver().getObjEntity(id.getEntityName());
        if (entity == null) {
            throw new IllegalArgumentException("Entity not mapped with Cayenne: " + id);
        }
        Persistent dataObject;
        try {
            dataObject = (Persistent) entity.getJavaClass().newInstance();
        } catch (Exception ex) {
            throw new CayenneRuntimeException("Error instantiating object.", ex);
        }
        dataObject.setObjectId(id);
        context.registerNewObject(dataObject);
    } finally {
        setExternalChange(Boolean.FALSE);
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjectId(org.apache.cayenne.ObjectId) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Persistent(org.apache.cayenne.Persistent) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 77 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class CayenneProjectPreferences method removeProjectDetailObject.

// delete property
public void removeProjectDetailObject(Preferences preference) {
    try {
        preference.removeNode();
        projectCayennePreferences.remove(preference);
    } catch (BackingStoreException e) {
        throw new CayenneRuntimeException("Error delete preferences", e);
    }
}
Also used : BackingStoreException(java.util.prefs.BackingStoreException) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 78 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class PropertyListSerialization method writeObject.

/**
 * Internal method to recursively write a property list object.
 */
protected static void writeObject(String offset, Writer out, Object plist) throws IOException {
    if (plist == null) {
        return;
    }
    if (plist instanceof Collection) {
        Collection list = (Collection) plist;
        out.write('\n');
        out.write(offset);
        if (list.size() == 0) {
            out.write("()");
            return;
        }
        out.write("(\n");
        String childOffset = offset + "   ";
        Iterator it = list.iterator();
        boolean appended = false;
        while (it.hasNext()) {
            // Java collections can contain nulls, skip them
            Object obj = it.next();
            if (obj != null) {
                if (appended) {
                    out.write(", \n");
                }
                out.write(childOffset);
                writeObject(childOffset, out, obj);
                appended = true;
            }
        }
        out.write('\n');
        out.write(offset);
        out.write(')');
    } else if (plist instanceof Map) {
        Map map = (Map) plist;
        out.write('\n');
        out.write(offset);
        if (map.size() == 0) {
            out.write("{}");
            return;
        }
        out.write("{");
        String childOffset = offset + "    ";
        Iterator it = map.entrySet().iterator();
        while (it.hasNext()) {
            // Java collections can contain nulls, skip them
            Map.Entry entry = (Map.Entry) it.next();
            Object key = entry.getKey();
            if (key == null) {
                continue;
            }
            Object obj = entry.getValue();
            if (obj == null) {
                continue;
            }
            out.write('\n');
            out.write(childOffset);
            out.write(quoteString(key.toString()));
            out.write(" = ");
            writeObject(childOffset, out, obj);
            out.write(';');
        }
        out.write('\n');
        out.write(offset);
        out.write('}');
    } else if (plist instanceof String) {
        out.write(quoteString(plist.toString()));
    } else if (plist instanceof Number) {
        out.write(plist.toString());
    } else {
        throw new CayenneRuntimeException("Unsupported class for property list serialization: " + plist.getClass().getName());
    }
}
Also used : Iterator(java.util.Iterator) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Collection(java.util.Collection) Map(java.util.Map)

Example 79 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class WOCompatCase method setupTestDirectory.

protected File setupTestDirectory(String subfolder) {
    String classPath = getClass().getName().replace('.', '/');
    String location = "target/testrun/" + classPath + "/" + subfolder;
    File testDirectory = new File(location);
    // delete old tests
    if (testDirectory.exists()) {
        if (!FileUtil.delete(location, true)) {
            throw new CayenneRuntimeException("Error deleting test directory '%s'", location);
        }
    }
    if (!testDirectory.mkdirs()) {
        throw new CayenneRuntimeException("Error creating test directory '%s'", location);
    }
    return testDirectory;
}
Also used : CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) File(java.io.File)

Example 80 with CayenneRuntimeException

use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.

the class EntityUtils method getMapKeyType.

/**
 * Returns the map key type for a collection relationship of type
 * java.util.Map.
 *
 * @param relationship
 *            The relationship to look up type information for.
 * @return The type of the attribute keyed on.
 */
public String getMapKeyType(final ObjRelationship relationship) {
    ObjEntity targetEntity = (ObjEntity) relationship.getTargetEntity();
    // key.
    if (relationship.getMapKey() == null) {
        // If it's a multi-column key, then the return type is always
        // ObjectId.
        DbEntity dbEntity = targetEntity.getDbEntity();
        if ((dbEntity != null) && (dbEntity.getPrimaryKeys().size() > 1)) {
            return ObjectId.class.getName();
        }
        // so default to Object.
        return Object.class.getName();
    }
    // If the map key is a non-default attribute, then fetch the attribute
    // and return
    // its type.
    ObjAttribute attribute = targetEntity.getAttribute(relationship.getMapKey());
    if (attribute == null) {
        throw new CayenneRuntimeException("Invalid map key '%s', no matching attribute found", relationship.getMapKey());
    }
    return attribute.getType();
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Aggregations

CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)168 Test (org.junit.Test)25 DbAttribute (org.apache.cayenne.map.DbAttribute)21 DataMap (org.apache.cayenne.map.DataMap)19 ObjectId (org.apache.cayenne.ObjectId)18 ObjEntity (org.apache.cayenne.map.ObjEntity)18 Persistent (org.apache.cayenne.Persistent)17 Expression (org.apache.cayenne.exp.Expression)17 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)17 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)14 DbEntity (org.apache.cayenne.map.DbEntity)14 IOException (java.io.IOException)13 List (java.util.List)12 ObjRelationship (org.apache.cayenne.map.ObjRelationship)12 DbRelationship (org.apache.cayenne.map.DbRelationship)10 DateTestEntity (org.apache.cayenne.testdo.date_time.DateTestEntity)10 File (java.io.File)9 Connection (java.sql.Connection)9 SQLException (java.sql.SQLException)9