Example usage for org.hibernate.collection.internal PersistentSet PersistentSet

List of usage examples for org.hibernate.collection.internal PersistentSet PersistentSet

Introduction

In this page you can find the example usage for org.hibernate.collection.internal PersistentSet PersistentSet.

Prototype

@Deprecated
public PersistentSet(SessionImplementor session, java.util.Set set) 

Source Link

Document

Instantiates a non-lazy set (the underlying set is constructed from the incoming set reference).

Usage

From source file:org.granite.hibernate4.HibernateExternalizer.java

License:Open Source License

protected PersistentCollection newHibernateCollection(AbstractExternalizablePersistentCollection value,
        Property field) {//from w ww.  j  ava2 s. c  o m
    final Type target = field.getType();
    final boolean initialized = value.isInitialized();
    final String metadata = value.getMetadata();
    final boolean dirty = value.isDirty();
    final boolean sorted = (SortedSet.class.isAssignableFrom(TypeUtil.classOfType(target))
            || SortedMap.class.isAssignableFrom(TypeUtil.classOfType(target)));

    Comparator<?> comparator = null;
    if (sorted && field.isAnnotationPresent(Sort.class)) {
        Sort sort = field.getAnnotation(Sort.class);
        if (sort.type() == SortType.COMPARATOR) {
            try {
                comparator = TypeUtil.newInstance(sort.comparator(), Comparator.class);
            } catch (Exception e) {
                throw new RuntimeException("Could not create instance of Comparator: " + sort.comparator());
            }
        }
    }

    PersistentCollection coll = null;
    if (value instanceof ExternalizablePersistentSet) {
        if (initialized) {
            Set<?> set = ((ExternalizablePersistentSet) value).getContentAsSet(target, comparator);
            coll = (sorted ? new PersistentSortedSet(null, (SortedSet<?>) set) : new PersistentSet(null, set));
        } else
            coll = (sorted ? new PersistentSortedSet() : new PersistentSet());
    } else if (value instanceof ExternalizablePersistentBag) {
        if (initialized) {
            List<?> bag = ((ExternalizablePersistentBag) value).getContentAsList(target);
            coll = new PersistentBag(null, bag);
        } else
            coll = new PersistentBag();
    } else if (value instanceof ExternalizablePersistentList) {
        if (initialized) {
            List<?> list = ((ExternalizablePersistentList) value).getContentAsList(target);
            coll = new PersistentList(null, list);
        } else
            coll = new PersistentList();
    } else if (value instanceof ExternalizablePersistentMap) {
        if (initialized) {
            Map<?, ?> map = ((ExternalizablePersistentMap) value).getContentAsMap(target, comparator);
            coll = (sorted ? new PersistentSortedMap(null, (SortedMap<?, ?>) map)
                    : new PersistentMap(null, map));
        } else
            coll = (sorted ? new PersistentSortedMap() : new PersistentMap());
    } else
        throw new RuntimeException("Illegal externalizable persitent class: " + value);

    if (metadata != null && serializeMetadata != SerializeMetadata.NO
            && (serializeMetadata == SerializeMetadata.YES || !initialized)) {
        String[] toks = metadata.split(":", 3);
        if (toks.length != 3)
            throw new RuntimeException("Invalid collection metadata: " + metadata);
        Serializable key = deserializeSerializable(StringUtil.hexStringToBytes(toks[0]));
        Serializable snapshot = deserializeSerializable(StringUtil.hexStringToBytes(toks[1]));
        String role = toks[2];
        coll.setSnapshot(key, role, snapshot);
    }

    if (initialized && dirty)
        coll.dirty();

    return coll;
}

From source file:org.granite.hibernate4.jmf.PersistentSetCodec.java

License:Open Source License

public PersistentSet newInstance(ExtendedObjectInput in, String className)
        throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException,
        InvocationTargetException, SecurityException, NoSuchMethodException {

    PersistentCollectionSnapshot snapshot = new JMFPersistentCollectionSnapshot(null);
    snapshot.readInitializationData(in);
    return (snapshot.isInitialized() ? new PersistentSet(null, new HashSet<Object>())
            : new PersistentSet(null));
}

From source file:org.granite.test.tide.hibernate4.data.AbstractTestChangeSetApplier.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test/*  w w  w .  j a v a  2  s. c om*/
public void testSimpleChanges() throws Exception {
    initPersistence();

    Person1 p = new Person1(null, null, "P1");
    p.setFirstName("test");
    p.setLastName("test");
    open();
    p = save(p);
    flush();
    Long personId = p.getId();
    close();

    open();

    InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(GRANITE_CONFIG_PATH);
    GraniteConfig graniteConfig = new GraniteConfig(null, is, null, "test");
    ServicesConfig servicesConfig = new ServicesConfig(null, null, false);
    SimpleGraniteContext.createThreadInstance(graniteConfig, servicesConfig, new HashMap<String, Object>());

    ChangeSet changeSet = new ChangeSet();
    Change change = new Change(Person1.class.getName(), personId, 0L, "P1");
    change.getChanges().put("firstName", "zozo");
    changeSet.setChanges(new Change[] { change });

    new ChangeSetApplier(newPersistenceAdapter()).applyChanges(changeSet);
    flush();

    p = find(Person1.class, personId);
    Assert.assertEquals("Property applied", "zozo", p.getFirstName());
    close();

    open();

    Person1 p0 = new Person1(personId, p.getVersion(), "P1");
    p0.setFirstName("zozo");
    p0.setLastName("test");
    p0.setContacts(new PersistentSet(null, new HashSet<Contact1>()));
    Address a0 = new Address(null, null, "A1");
    a0.setCity("New York City");
    Contact1 c0 = new Contact1(null, null, "C1");
    c0.setPerson(p0);
    c0.setAddress(a0);
    c0.setEmail("zozo@zozo.net");
    p0.getContacts().add(c0);

    ChangeSet changeSet2 = new ChangeSet();
    Change change2 = new Change(Person1.class.getName(), personId, p.getVersion(), "P1");
    CollectionChanges collChanges2 = new CollectionChanges();
    CollectionChange collChange2 = new CollectionChange(1, null, c0);
    collChanges2.setChanges(new CollectionChange[] { collChange2 });
    change2.getChanges().put("contacts", collChanges2);
    changeSet2.setChanges(new Change[] { change2 });

    Object[] changes = new ChangeSetApplier(newPersistenceAdapter()).applyChanges(changeSet2);
    Assert.assertEquals("Changes", 1, changes.length);
    Assert.assertTrue("Changed person", changes[0] instanceof Person1);
    Person1 pc = (Person1) changes[0];
    Assert.assertEquals("Changed person coll", 1, pc.getContacts().size());
    Assert.assertEquals("Changed person coll element", pc, pc.getContacts().iterator().next().getPerson());

    flush();

    p = find(Person1.class, personId);

    Assert.assertEquals("Collection added", 1, p.getContacts().size());
    Contact1 contact = p.getContacts().iterator().next();
    Long contactId = contact.getId();
    Long addressId = contact.getAddress().getId();
    Assert.assertEquals("Element property", "zozo@zozo.net", contact.getEmail());
    close();

    open();

    Person1 p1 = new Person1(personId, p.getVersion(), "P1");
    p1.setFirstName("zozo");
    p1.setLastName("zozo");
    p1.setContacts(new PersistentSet(null, new HashSet<Contact1>()));
    Address a1 = new Address(addressId, 0L, "A1");
    a1.setCity("New York City");
    Contact1 c1 = new Contact1(contactId, 0L, "C1");
    c1.setPerson(p1);
    c1.setAddress(a1);
    c1.setEmail("zozo@zozo.net");
    p1.getContacts().add(c1);
    Address a2 = new Address(null, null, "A2");
    a2.setCity("Paris");
    Contact1 c2 = new Contact1(null, null, "C2");
    c2.setPerson(p1);
    c2.setAddress(a2);
    c2.setEmail("zozo@zozo.fr");
    p1.getContacts().add(c2);

    ChangeSet changeSet3 = new ChangeSet();
    Change change3 = new Change(Person1.class.getName(), personId, p.getVersion(), "P1");
    CollectionChanges collChanges3 = new CollectionChanges();
    CollectionChange collChange3 = new CollectionChange(1, null, c2);
    collChanges3.setChanges(new CollectionChange[] { collChange3 });
    change3.getChanges().put("contacts", collChanges3);
    change3.getChanges().put("lastName", "zozo");
    changeSet3.setChanges(new Change[] { change3 });

    Object[] changes2 = new ChangeSetApplier(newPersistenceAdapter()).applyChanges(changeSet3);
    Assert.assertEquals("Changes 2", 1, changes2.length);
    Assert.assertTrue("Changed person 2", changes2[0] instanceof Person1);
    Person1 pc2 = (Person1) changes2[0];
    Assert.assertEquals("Changed person 2 coll", 2, pc2.getContacts().size());
    for (Contact1 c : pc2.getContacts())
        Assert.assertEquals("Changed person 2 coll element", pc2, c.getPerson());

    flush();

    p = find(Person1.class, personId);

    Assert.assertEquals("Person 2 property", "zozo", p.getLastName());
    Assert.assertEquals("Collection 2 added", 2, p.getContacts().size());
    close();

    open();

    Person1 p2 = newProxy(Person1.class, personId);
    Address a3 = new Address(null, null, "A3");
    a3.setCity("London");
    Contact1 c3 = new Contact1(null, null, "C3");
    c3.setPerson(p2);
    c3.setAddress(a3);
    c3.setEmail("zozo@zozo.co.uk");

    ChangeSet changeSet4 = new ChangeSet();
    Change change4 = new Change(Person1.class.getName(), personId, p.getVersion(), "P1");
    CollectionChanges collChanges4 = new CollectionChanges();
    CollectionChange collChange4 = new CollectionChange(1, null, c3);
    collChanges4.setChanges(new CollectionChange[] { collChange4 });
    change4.getChanges().put("contacts", collChanges4);
    changeSet4.setChanges(new Change[] { change4 });

    Object[] changes3 = new ChangeSetApplier(newPersistenceAdapter()).applyChanges(changeSet4);
    Assert.assertEquals("Changes 3", 1, changes3.length);
    Assert.assertTrue("Changed person 3", changes3[0] instanceof Person1);
    Person1 pc3 = (Person1) changes3[0];
    Assert.assertEquals("Changed person 3 coll", 3, pc3.getContacts().size());
    for (Contact1 c : pc3.getContacts())
        Assert.assertSame("Changed person 3 coll element", pc3, c.getPerson());

    flush();

    p = find(Person1.class, personId);

    Assert.assertEquals("Person 3 property", "zozo", p.getLastName());
    Assert.assertEquals("Collection 3 added", 3, p.getContacts().size());
    for (Contact1 c : p.getContacts())
        Assert.assertSame("Associations", p, c.getPerson());
    close();
}

From source file:org.granite.test.tide.hibernate4.data.AbstractTestChangeSetApplier.java

License:Open Source License

@Test
@SuppressWarnings("unchecked")
public void testProxyChanges() throws Exception {
    initPersistence();/*from ww  w.  j  av a2 s. co  m*/

    open();

    Classification cl2 = new Classification(null, null, "CL2");
    cl2.setCode("CL2");
    cl2 = save(cl2);

    flush();

    Long id2 = cl2.getId();
    Long v2 = cl2.getVersion();

    close();

    ProxyFactory pf = new ProxyFactory(JavassistLazyInitializer.class.getName());
    cl2 = (Classification) pf.getProxyInstance(Classification.class.getName(), Classification.class.getName(),
            id2);

    open();

    InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(GRANITE_CONFIG_PATH);
    GraniteConfig graniteConfig = new GraniteConfig(null, is, null, "test");
    ServicesConfig servicesConfig = new ServicesConfig(null, null, false);
    SimpleGraniteContext.createThreadInstance(graniteConfig, servicesConfig, new HashMap<String, Object>());

    Classification cl1 = new Classification(null, null, "CL1");
    cl1.setCode("CL1");
    cl1.setSubClassifications(new PersistentList(null, new ArrayList<Classification>()));
    cl1.setSuperClassifications(new PersistentSet(null, new HashSet<Classification>()));
    cl1.getSubClassifications().add(cl2);

    ChangeSet changeSet = new ChangeSet();
    Change change1 = new Change(Classification.class.getName(), id2, v2, "CL2");
    CollectionChanges collChanges1 = new CollectionChanges();
    CollectionChange collChange1 = new CollectionChange(1, null, cl1);
    collChanges1.setChanges(new CollectionChange[] { collChange1 });
    change1.getChanges().put("superClassifications", collChanges1);
    changeSet.setChanges(new Change[] { change1 });

    new ChangeSetApplier(newPersistenceAdapter()).applyChanges(changeSet);
    flush();
    close();

    open();

    cl2 = find(Classification.class, id2);
    Assert.assertEquals("Collection change applied", 1, cl2.getSuperClassifications().size());
    Assert.assertSame("Collection change applied 2", cl2,
            cl2.getSuperClassifications().iterator().next().getSubClassifications().get(0));
    close();
}

From source file:org.granite.test.tide.hibernate4.data.AbstractTestChangeSetApplier.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test/*from  w ww .  ja  va  2 s.co m*/
public void testSimpleChangesListAdd() throws Exception {
    initPersistence();

    open();

    Patient p = new Patient(null, null, "P1");
    p.setName("Chuck Norris");
    p.setMedicationList(new PersistentSet(null, new HashSet<Medication>()));
    Medication m = new Medication(null, null, "M1");
    m.setName("Aspirin");
    m.setPatient(p);
    m.setPrescriptionList(new PersistentSet(null, new HashSet<Prescription>()));
    p.getMedicationList().add(m);
    Prescription pr = new Prescription(null, null, "PR1");
    pr.setName("500 mg");
    pr.setMedication(m);
    m.getPrescriptionList().add(pr);

    open();
    p = save(p);
    flush();
    Long patientId = p.getId();
    close();

    open();

    InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(GRANITE_CONFIG_PATH);
    GraniteConfig graniteConfig = new GraniteConfig(null, is, null, "test");
    ServicesConfig servicesConfig = new ServicesConfig(null, null, false);
    SimpleGraniteContext.createThreadInstance(graniteConfig, servicesConfig, new HashMap<String, Object>());
    DataContext.init(null, null, PublishMode.MANUAL);

    ChangeSet changeSet = new ChangeSet();
    Change change = new Change(Patient.class.getName(), patientId, p.getVersion(), "P1");
    Patient p2 = new Patient(p.getId(), p.getVersion(), "P1");
    p2.setName(p.getName());
    p2.setMedicationList(new PersistentSet());
    Medication m2 = new Medication(null, null, "M2");
    m2.setName("Xanax");
    m2.setPatient(p2);
    m2.setPrescriptionList(new PersistentSet(null, new HashSet<Prescription>()));
    Prescription pr2 = new Prescription(null, null, "PR2");
    pr2.setName("1 kg");
    pr2.setMedication(m2);
    m2.getPrescriptionList().add(pr2);
    change.getChanges().put("medicationList",
            new CollectionChanges(new CollectionChange[] { new CollectionChange(1, 1, m2) }));
    changeSet.setChanges(new Change[] { change });

    new ChangeSetApplier(newPersistenceAdapter()).applyChanges(changeSet);
    flush();

    Patient p3 = find(Patient.class, patientId);

    Assert.assertEquals("List updated", 2, p3.getMedicationList().size());
    boolean found = false;
    for (Medication m3 : p3.getMedicationList()) {
        if (m3.getUid().equals("M2"))
            found = true;
    }
    Assert.assertTrue("Medication 2", found);

    Object[][] updates = DataContext.get().getUpdates();

    Assert.assertNotNull("Updates", updates);

    close();
}

From source file:org.granite.test.tide.hibernate4.data.AbstractTestChangeSetApplier.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test//from ww  w.j a va 2 s  . co m
public void testSimpleChangesListAdd2() throws Exception {
    initPersistence();

    open();

    Patient p = new Patient(null, null, "P1");
    p.setName("Chuck Norris");
    p.setMedicationList(new PersistentSet(null, new HashSet<Medication>()));
    Medication m = new Medication(null, null, "M1");
    m.setName("Aspirin");
    m.setPatient(p);
    m.setPrescriptionList(new PersistentSet(null, new HashSet<Prescription>()));
    p.getMedicationList().add(m);
    Prescription pr = new Prescription(null, null, "PR1");
    pr.setName("500 mg");
    pr.setMedication(m);
    m.getPrescriptionList().add(pr);

    open();
    p = save(p);
    flush();
    Long patientId = p.getId();
    close();

    open();

    InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(GRANITE_CONFIG_PATH);
    GraniteConfig graniteConfig = new GraniteConfig(null, is, null, "test");
    ServicesConfig servicesConfig = new ServicesConfig(null, null, false);
    SimpleGraniteContext.createThreadInstance(graniteConfig, servicesConfig, new HashMap<String, Object>());
    DataContext.init(null, null, PublishMode.MANUAL);

    ChangeSet changeSet = new ChangeSet();
    Change change = new Change(Patient.class.getName(), patientId, p.getVersion(), "P1");
    Patient p2 = new Patient(p.getId(), p.getVersion(), "P1");
    p2.setName(p.getName());
    p2.setMedicationList(new PersistentSet(null, new HashSet<Medication>()));
    Medication m1 = new Medication(m.getId(), m.getVersion(), "M1");
    m1.setName(m.getName());
    m1.setPatient(p2);
    m1.setPrescriptionList(new PersistentSet(null, new HashSet<Prescription>()));
    Prescription pr1 = new Prescription(pr.getId(), pr.getVersion(), "PR1");
    pr1.setName(pr.getName());
    pr1.setMedication(m1);
    p2.getMedicationList().add(m1);
    Medication m2 = new Medication(null, null, "M2");
    m2.setName("Xanax");
    m2.setPatient(p2);
    m2.setPrescriptionList(new PersistentSet(null, new HashSet<Prescription>()));
    Prescription pr2 = new Prescription(null, null, "PR2");
    pr2.setName("1 kg");
    pr2.setMedication(m2);
    m2.getPrescriptionList().add(pr2);
    p2.getMedicationList().add(m2);
    change.getChanges().put("medicationList",
            new CollectionChanges(new CollectionChange[] { new CollectionChange(1, 1, m2) }));
    changeSet.setChanges(new Change[] { change });

    new ChangeSetApplier(newPersistenceAdapter()).applyChanges(changeSet);
    flush();

    Patient p3 = find(Patient.class, patientId);

    Assert.assertEquals("List updated", 2, p3.getMedicationList().size());
    boolean found = false;
    for (Medication m3 : p3.getMedicationList()) {
        if (m3.getUid().equals("M2"))
            found = true;
    }
    Assert.assertTrue("Medication 2", found);

    Object[][] updates = DataContext.get().getUpdates();

    Assert.assertNotNull("Updates", updates);

    close();
}

From source file:org.granite.test.tide.hibernate4.data.AbstractTestChangeSetApplier.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test//from w ww  . j  a v a2  s. co m
public void testSimpleChangesListAdd3() throws Exception {
    initPersistence();

    open();

    Patient p = new Patient(null, null, "P1");
    p.setName("Chuck Norris");
    p.setMedicationList(new PersistentSet(null, new HashSet<Medication>()));
    Medication m = new Medication(null, null, "M1");
    m.setName("Aspirin");
    m.setPatient(p);
    m.setPrescriptionList(new PersistentSet(null, new HashSet<Prescription>()));
    p.getMedicationList().add(m);
    Prescription pr = new Prescription(null, null, "PR1");
    pr.setName("500 mg");
    pr.setMedication(m);
    m.getPrescriptionList().add(pr);

    open();
    p = save(p);
    flush();
    Long patientId = p.getId();
    close();

    open();

    InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(GRANITE_CONFIG_PATH);
    GraniteConfig graniteConfig = new GraniteConfig(null, is, null, "test");
    ServicesConfig servicesConfig = new ServicesConfig(null, null, false);
    SimpleGraniteContext.createThreadInstance(graniteConfig, servicesConfig, new HashMap<String, Object>());
    DataContext.init(null, null, PublishMode.MANUAL);

    ChangeSet changeSet = new ChangeSet();
    Change change = new Change(Patient.class.getName(), patientId, p.getVersion(), "P1");
    Patient p2 = new Patient(p.getId(), p.getVersion(), "P1");
    p2.setName(p.getName());
    p2.setMedicationList(new PersistentSet(null, new HashSet<Medication>()));
    Medication m1 = new Medication(m.getId(), m.getVersion(), "M1");
    m1.setName(m.getName());
    m1.setPatient(p2);
    m1.setPrescriptionList(new PersistentSet());
    p2.getMedicationList().add(m1);
    Medication m2 = new Medication(null, null, "M2");
    m2.setName("Xanax");
    m2.setPatient(p2);
    m2.setPrescriptionList(new PersistentSet(null, new HashSet<Prescription>()));
    Prescription pr2 = new Prescription(null, null, "PR2");
    pr2.setName("1 kg");
    pr2.setMedication(m2);
    m2.getPrescriptionList().add(pr2);
    p2.getMedicationList().add(m2);
    change.getChanges().put("medicationList",
            new CollectionChanges(new CollectionChange[] { new CollectionChange(1, 1, m2) }));
    changeSet.setChanges(new Change[] { change });

    new ChangeSetApplier(newPersistenceAdapter()).applyChanges(changeSet);
    flush();

    Patient p3 = find(Patient.class, patientId);

    Assert.assertEquals("List updated", 2, p3.getMedicationList().size());
    boolean found = false;
    for (Medication m3 : p3.getMedicationList()) {
        if (m3.getUid().equals("M2"))
            found = true;
    }
    Assert.assertTrue("Medication 2", found);

    Object[][] updates = DataContext.get().getUpdates();

    Assert.assertNotNull("Updates", updates);

    close();
}

From source file:org.granite.test.tide.hibernate4.data.AbstractTestChangeSetApplier.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test//from  ww w  . ja v  a  2 s .  c om
public void testSimpleChangesListAdd5() throws Exception {
    initPersistence();

    open();

    Patient p = new Patient(null, null, "P1");
    p.setName("Chuck Norris");
    p.setMedicationList(new PersistentSet(null, new HashSet<Medication>()));
    Medication m = new Medication(null, null, "M1");
    m.setName("Aspirin");
    m.setPatient(p);
    m.setPrescriptionList(new PersistentSet(null, new HashSet<Prescription>()));
    p.getMedicationList().add(m);
    Prescription pr = new Prescription(null, null, "PR1");
    pr.setName("500 mg");
    pr.setMedication(m);
    m.getPrescriptionList().add(pr);

    open();
    p = save(p);
    flush();
    Long patientId = p.getId();
    close();

    open();

    InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(GRANITE_CONFIG_PATH);
    GraniteConfig graniteConfig = new GraniteConfig(null, is, null, "test");
    ServicesConfig servicesConfig = new ServicesConfig(null, null, false);
    SimpleGraniteContext.createThreadInstance(graniteConfig, servicesConfig, new HashMap<String, Object>());
    DataContext.init(null, null, PublishMode.MANUAL);

    ChangeSet changeSet = new ChangeSet();
    Change change = new Change(Patient.class.getName(), patientId, p.getVersion(), "P1");
    Patient p2 = new Patient(p.getId(), p.getVersion(), "P1");
    p2.setName(p.getName());
    p2.setMedicationList(new PersistentSet());
    Medication m2 = new Medication(null, null, "M2");
    m2.setName("Xanax");
    m2.setPatient(p2);
    m2.setPrescriptionList(new PersistentSet(null, new HashSet<Prescription>()));
    Prescription pr2 = new Prescription(null, null, "PR2");
    pr2.setName("1 kg");
    pr2.setMedication(m2);
    m2.getPrescriptionList().add(pr2);
    change.getChanges().put("medicationList",
            new CollectionChanges(new CollectionChange[] { new CollectionChange(1, 1, m2) }));
    changeSet.setChanges(new Change[] { change });

    new ChangeSetApplier(newPersistenceAdapter()).applyChanges(changeSet);
    flush();

    Patient p3 = find(Patient.class, patientId);

    Assert.assertEquals("List updated", 2, p3.getMedicationList().size());
    Medication m3 = null;
    for (Medication mm : p3.getMedicationList()) {
        if (mm.getUid().equals("M2")) {
            m3 = mm;
            break;
        }
    }
    Assert.assertNotNull("Medication 2", m3);
    Assert.assertEquals("Medication prescriptions", 1, m3.getPrescriptionList().size());

    close();
}

From source file:org.granite.test.tide.hibernate4.data.AbstractTestChangeSetApplier.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test/*from  w  ww .ja  v  a  2 s . c  om*/
public void testSimpleChangesListAdd6() throws Exception {
    initPersistence();

    open();

    Patient p = new Patient(null, null, "P1");
    p.setName("Chuck Norris");
    p.setMedicationList(new PersistentSet(null, new HashSet<Medication>()));
    Medication m = new Medication(null, null, "M1");
    m.setName("Aspirin");
    m.setPatient(p);
    m.setPrescriptionList(new PersistentSet(null, new HashSet<Prescription>()));
    p.getMedicationList().add(m);
    Prescription pr = new Prescription(null, null, "PR1");
    pr.setName("500 mg");
    pr.setMedication(m);
    m.getPrescriptionList().add(pr);

    open();
    p = save(p);
    flush();
    Long patientId = p.getId();
    m = p.getMedicationList().iterator().next();
    close();

    open();

    InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(GRANITE_CONFIG_PATH);
    GraniteConfig graniteConfig = new GraniteConfig(null, is, null, "test");
    ServicesConfig servicesConfig = new ServicesConfig(null, null, false);
    SimpleGraniteContext.createThreadInstance(graniteConfig, servicesConfig, new HashMap<String, Object>());
    DataContext.init(null, null, PublishMode.MANUAL);

    ChangeSet changeSet = new ChangeSet();
    Change change = new Change(Medication.class.getName(), m.getId(), m.getVersion(), m.getUid());
    Patient p2 = new Patient(p.getId(), p.getVersion(), p.getUid());
    p2.setName(p.getName());
    p2.setMedicationList(new PersistentSet());
    Medication m2 = new Medication(m.getId(), m.getVersion(), m.getUid());
    m2.setName("Aspirin");
    m2.setPatient(p2);
    m2.setPrescriptionList(new PersistentSet());
    Prescription pr2 = new Prescription(null, null, "PR2");
    pr2.setName("1 kg");
    pr2.setMedication(m2);
    change.getChanges().put("prescriptionList",
            new CollectionChanges(new CollectionChange[] { new CollectionChange(1, 1, pr2) }));
    changeSet.setChanges(new Change[] { change });

    new ChangeSetApplier(newPersistenceAdapter()).applyChanges(changeSet);
    flush();

    Patient p3 = find(Patient.class, patientId);
    Medication m3 = p3.getMedicationList().iterator().next();

    Assert.assertEquals("List updated", 2, m3.getPrescriptionList().size());
    Prescription pr3 = null;
    for (Prescription prr : m3.getPrescriptionList()) {
        if (prr.getUid().equals("PR2")) {
            pr3 = prr;
            break;
        }
    }
    Assert.assertNotNull("Prescription 2", pr3);
    Assert.assertEquals("Medication prescriptions", 2, m3.getPrescriptionList().size());

    close();
}

From source file:org.granite.test.tide.hibernate4.data.AbstractTestChangeSetApplier.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test/*from w  ww  .  j  a va 2s. c  om*/
public void testMultipleSetAddNew() throws Exception {
    initPersistence();

    open();

    Patient2 p = new Patient2(null, null, "P1");
    p.setName("Chuck Norris");
    p.setTests(new PersistentSet(null, new HashSet<Test2>()));
    p.setVisits(new PersistentSet(null, new HashSet<Visit2>()));
    Visit2 v = new Visit2(null, null, "V2");
    v.setName("Visit");
    v.setPatient(p);
    v.setTests(new PersistentSet(null, new HashSet<Test2>()));
    p.getVisits().add(v);
    VitalSignTest2 vst = new VitalSignTest2(null, null, "VST1");
    vst.setName("Test");
    vst.setPatient(p);
    vst.setVisit(v);
    v.getTests().add(vst);

    open();
    p = save(p);
    flush();
    Long patientId = p.getId();
    v = p.getVisits().iterator().next();
    close();

    open();

    InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(GRANITE_CONFIG_PATH);
    GraniteConfig graniteConfig = new GraniteConfig(null, is, null, "test");
    ServicesConfig servicesConfig = new ServicesConfig(null, null, false);
    SimpleGraniteContext.createThreadInstance(graniteConfig, servicesConfig, new HashMap<String, Object>());
    DataContext.init(null, null, PublishMode.MANUAL);

    Change change = new Change(Visit2.class.getName(), v.getId(), v.getVersion(), v.getUid());

    Patient2 p2 = new Patient2(p.getId(), p.getVersion(), p.getUid());
    p2.setName(p.getName());
    p2.setTests(new PersistentSet());
    p2.setVisits(new PersistentSet(null, new HashSet<Visit2>()));

    Visit2 v2 = new Visit2(v.getId(), v.getVersion(), v.getUid());
    v2.setName(v.getName());
    v2.setTests(new PersistentSet(null, new HashSet<Test2>()));
    p2.getVisits().add(v2);

    VitalSignTest2 vst2 = new VitalSignTest2();
    vst2.setVitalSignObservations(new PersistentSet(null, new HashSet<VitalSignObservation2>()));
    VitalSignObservation2 vso2a = new VitalSignObservation2();
    vso2a.setVitalSignTest(vst2);
    VitalSignObservation2 vso2b = new VitalSignObservation2();
    vso2b.setVitalSignTest(vst2);
    vst2.setPatient(p2);
    vst2.setVisit(v2);
    vst2.getVitalSignObservations().add(vso2a);
    vst2.getVitalSignObservations().add(vso2b);
    v2.getTests().add(vst2);

    CollectionChange collChange = new CollectionChange(1, 0, vst2);
    change.addCollectionChanges("tests", new CollectionChange[] { collChange });

    Change change2 = new Change(Visit2.class.getName(), v.getId(), v.getVersion(), v.getUid());
    change2.getChanges().put("version", v.getVersion());

    ChangeSet changeSet = new ChangeSet(new Change[] { change });

    new ChangeSetApplier(newPersistenceAdapter()).applyChanges(changeSet);
    flush();

    Patient2 p3 = find(Patient2.class, patientId);

    VitalSignTest2 vst3 = null;
    for (Test2 t : p3.getVisits().iterator().next().getTests()) {
        if (!t.getUid().equals("VST1")) {
            vst3 = (VitalSignTest2) t; // New test will not have the uid for the first one
            break;
        }
    }
    Assert.assertNotNull("Test saved", vst3.getId());
    Assert.assertEquals("Observations", 2, vst3.getVitalSignObservations().size());

    Object[][] updates = DataContext.get().getUpdates();

    Assert.assertNotNull("Updates", updates);

    close();
}