Esempio n. 1
0
  @Test
  public void testDataRowIntPk() throws Exception {
    createTwoArtists();

    DataRow a3 = SelectById.dataRowQuery(Artist.class, 3).selectOne(context);
    assertNotNull(a3);
    assertEquals("artist3", a3.get("ARTIST_NAME"));

    DataRow a2 = SelectById.dataRowQuery(Artist.class, 2).selectOne(context);
    assertNotNull(a2);
    assertEquals("artist2", a2.get("ARTIST_NAME"));
  }
    public void nextRows(Query query, List<?> dataRows) {

      // process selected object, issue an update query
      if (dataRows == null || dataRows.size() == 0) {
        throw new CayenneRuntimeException(
            "Error generating PK : entity not supported: " + entityName);
      }

      if (dataRows.size() > 1) {
        throw new CayenneRuntimeException(
            "Error generating PK : too many rows for entity: " + entityName);
      }

      DataRow lastPk = (DataRow) dataRows.get(0);
      id = (Number) lastPk.get("NEXT_ID");
    }
Esempio n. 3
0
  /**
   * プロジェクト進捗率を取得します。 最上位のタスクより進捗率を計算します。
   *
   * @param projectId プロジェクトID
   * @return 紐づく全タスク情報
   */
  public static int getProjectProgressRate(Integer projectId) {

    try {

      List<DataRow> result = getProjectProgress(projectId);

      if (result == null) {
        return 0;
      }

      DataRow row = result.get(0);
      Object per = row.get("result_per");
      if (per == null) {
        return 0;
      }

      return Integer.valueOf(row.get("result_per").toString());

    } catch (Exception ex) {
      logger.error("Exception", ex);
      return 0;
    }
  }
Esempio n. 4
0
  /**
   * Returns a DataRow reflecting current, possibly uncommitted, object state.
   *
   * <p><strong>Warning:</strong> This method will return a partial snapshot if an object or one of
   * its related objects that propagate their keys to this object have temporary ids. DO NOT USE
   * this method if you expect a DataRow to represent a complete object state.
   *
   * @since 1.1
   */
  public DataRow currentSnapshot(final Persistent object) {

    // for a HOLLOW object return snapshot from cache
    if (object.getPersistenceState() == PersistenceState.HOLLOW
        && object.getObjectContext() != null) {

      return getObjectStore().getSnapshot(object.getObjectId());
    }

    ObjEntity entity = getEntityResolver().getObjEntity(object);
    final ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(entity.getName());
    final DataRow snapshot = new DataRow(10);
    snapshot.setEntityName(entity.getName());

    descriptor.visitProperties(
        new PropertyVisitor() {

          public boolean visitAttribute(AttributeProperty property) {
            ObjAttribute objAttr = property.getAttribute();

            // processing compound attributes correctly
            snapshot.put(objAttr.getDbAttributePath(), property.readPropertyDirectly(object));
            return true;
          }

          public boolean visitToMany(ToManyProperty property) {
            // do nothing
            return true;
          }

          public boolean visitToOne(ToOneProperty property) {
            ObjRelationship rel = property.getRelationship();

            // if target doesn't propagates its key value, skip it
            if (rel.isSourceIndependentFromTargetChange()) {
              return true;
            }

            Object targetObject = property.readPropertyDirectly(object);
            if (targetObject == null) {
              return true;
            }

            // if target is Fault, get id attributes from stored snapshot
            // to avoid unneeded fault triggering
            if (targetObject instanceof Fault) {
              DataRow storedSnapshot = getObjectStore().getSnapshot(object.getObjectId());
              if (storedSnapshot == null) {
                throw new CayenneRuntimeException(
                    "No matching objects found for ObjectId "
                        + object.getObjectId()
                        + ". Object may have been deleted externally.");
              }

              DbRelationship dbRel = rel.getDbRelationships().get(0);
              for (DbJoin join : dbRel.getJoins()) {
                String key = join.getSourceName();
                snapshot.put(key, storedSnapshot.get(key));
              }

              return true;
            }

            // target is resolved and we have an FK->PK to it,
            // so extract it from target...
            Persistent target = (Persistent) targetObject;
            Map<String, Object> idParts = target.getObjectId().getIdSnapshot();

            // this may happen in uncommitted objects - see the warning in
            // the JavaDoc
            // of
            // this method.
            if (idParts.isEmpty()) {
              return true;
            }

            DbRelationship dbRel = rel.getDbRelationships().get(0);
            Map<String, Object> fk = dbRel.srcFkSnapshotWithTargetSnapshot(idParts);
            snapshot.putAll(fk);
            return true;
          }
        });

    // process object id map
    // we should ignore any object id values if a corresponding attribute
    // is a part of relationship "toMasterPK", since those values have been
    // set above when db relationships where processed.
    Map<String, Object> thisIdParts = object.getObjectId().getIdSnapshot();
    if (thisIdParts != null) {

      // put only those that do not exist in the map
      for (Map.Entry<String, Object> entry : thisIdParts.entrySet()) {
        String nextKey = entry.getKey();
        if (!snapshot.containsKey(nextKey)) {
          snapshot.put(nextKey, entry.getValue());
        }
      }
    }

    return snapshot;
  }