예제 #1
0
  public void testArticleImageLink() {
    Session ssn = SessionManager.getSession();
    DataObject article = ssn.create(getModel() + ".Article");
    article.set("id", BigInteger.ZERO);
    String text = "This is the article text.";
    article.set("text", text);

    for (int i = 0; i < 10; i++) {
      DataObject image = ssn.create(getModel() + ".Image");
      image.set("id", new BigInteger(Integer.toString(i)));
      byte[] bytes = "This is the image.".getBytes();
      image.set("bytes", bytes);
      image.save();
    }

    DataAssociation links = (DataAssociation) article.get("images");
    DataCollection images = ssn.retrieve(getModel() + ".Image");
    while (images.next()) {
      DataObject image = images.getDataObject();
      DataObject link = ssn.create(getModel() + ".ArticleImageLink");
      link.set("article", article);
      link.set("image", image);
      link.set("caption", "The caption for: " + image.getOID());
      links.add(link);
    }

    article.save();

    DataAssociationCursor cursor = links.cursor();
    assertEquals(10, cursor.size());

    DataCollection aiLinks = ssn.retrieve(getModel() + ".ArticleImageLink");
    aiLinks.addEqualsFilter("image.id", new BigDecimal(5));
    if (aiLinks.next()) {
      DataObject linkArticle = (DataObject) aiLinks.get("article");
      DataObject linkImage = (DataObject) aiLinks.get("image");
      String caption = (String) aiLinks.get("caption");
      assertEquals(BigInteger.valueOf(0), linkArticle.get("id"));
      assertEquals(BigInteger.valueOf(5), linkImage.get("id"));

      if (aiLinks.next()) {
        fail("too many rows");
      }
    } else {
      fail("no rows returned");
    }

    article.delete();
  }
  public void testDeepLink() {
    Session ssn = SessionManager.getSession();

    DataObject[] users = new DataObject[4];

    DataObject group = getSession().create(getModelName() + ".Group");
    group.set("id", BigInteger.valueOf(users.length));
    group.set("email", "*****@*****.**");
    group.set("name", "SIPB");
    group.save();
    DataAssociation members = (DataAssociation) group.get("members");

    for (int i = 0; i < users.length; i++) {
      users[i] = ssn.create(getModelName() + ".User");
      users[i].set("id", BigInteger.valueOf(i));
      users[i].set("email", "*****@*****.**");
      users[i].set("firstName", "foo");
      users[i].set("lastNames", "bar");
      users[i].save();
      members.add(users[i]);
    }
    group.save();

    DataObject[] images = new DataObject[users.length / 2];
    for (int i = 0; i < images.length; i++) {
      images[i] = ssn.create(getModelName() + ".Image");
      images[i].set("id", BigInteger.valueOf(i));
      byte[] bytes = "This is the image.".getBytes();
      images[i].set("bytes", bytes);
      images[i].save();
    }

    // create link between user i and image i/2 with caption i
    for (int i = 0; i < users.length; i++) {
      // set image
      DataAssociation imageUsers = (DataAssociation) images[i / 2].get("users");
      DataObject link = imageUsers.add(users[i]);
      link.set("caption", String.valueOf(i));
      link.save();
    }

    DataCollection dc = ssn.retrieve(getModelName() + ".Group");
    dc.addEqualsFilter("members.image.link.caption", "0");
    assertEquals(1, dc.size());

    dc = ssn.retrieve(getModelName() + ".User");
    dc.addPath("image.link.caption");
    assertEquals(users.length, dc.size());
    while (dc.next()) {
      assertEquals(dc.get("id").toString(), dc.get("image.link.caption"));
    }

    dc = ssn.retrieve(getModelName() + ".User");
    dc.addPath("image.id");
    assertEquals(users.length, dc.size());
    while (dc.next()) {
      int id = ((BigInteger) dc.get("id")).intValue();
      assertEquals(BigInteger.valueOf(id / 2), dc.get("image.id"));
    }

    DataCollection dcUp = ssn.retrieve(getModelName() + ".User");
    DataCollection dcDown = ssn.retrieve(getModelName() + ".User");
    dcUp.addOrder("image.link.caption asc");
    dcDown.addOrder("image.link.caption desc");
    dcUp.next();
    dcDown.next();
    assertEquals(BigInteger.valueOf(0), dcUp.get("id"));
    assertEquals(BigInteger.valueOf(users.length - 1), dcDown.get("id"));
    dcUp.close();
    dcDown.close();

    dcUp = ssn.retrieve(getModelName() + ".Image");
    dcDown = ssn.retrieve(getModelName() + ".Image");
    dcUp.addOrder("users.link.caption asc");
    dcDown.addOrder("users.link.caption desc");
    dcUp.next();
    dcDown.next();
    assertEquals(BigInteger.valueOf(0), dcUp.get("id"));
    assertEquals(BigInteger.valueOf(images.length - 1), dcDown.get("id"));
    dcUp.close();
    dcDown.close();

    dc = ssn.retrieve(getModelName() + ".Group");
    dc.addFilter("members.image.id = 0");
    assertEquals(2, dc.size());

    dc = ssn.retrieve(getModelName() + ".Image");
    dc.addFilter("users.id = 0 and users.link.caption = '1'");
    assertEquals(0, dc.size());

    dc = ssn.retrieve(getModelName() + ".Group");
    dc.addPath("members.id");
    dc.addFilter("members.image.id = 0 and members.image.link.caption = '1'");
    assertTrue(dc.next());
    assertEquals(BigInteger.valueOf(1), dc.get("members.id"));
    assertFalse(dc.next());
  }