public static boolean contains(Collection<?> collection, Object obj) { if (collection != null) { if (collection instanceof PersistentSet) { for (Object elto : collection) { if (elto.equals(obj)) { return true; } } return false; } else { return collection.contains(obj); } } else { return false; } }
@SuppressWarnings("unchecked") @Test public void testPersistentSetsAreConverted() throws Exception { // We create a initialized persistentSet with valid snapshot. PersistentSet set = new PersistentSet(null, new HashSet<>()); set.setSnapshot(null, null, new HashMap<Object, Object>()); invokeMethod("myMethod2ParamPrimitifResultPrimitif", 45.2d, new TestedBean("coucou", 7, 8, null, set)); assertTestIs("@Test" + "public void testmyMethod2ParamPrimitifResultPrimitif_" + nextTestId() + "() throws Exception {" + "//Call to tested method" + "Object[] inputParams_enter = new Object[]{" + "JsonSerialisationUtils.deserialize(\"{\\\"@type\\\":\\\"double\\\",\\\"value\\\":45.2}\"), " + "JsonSerialisationUtils.deserialize(\"{\\\"@type\\\":\\\"com.freeyourcode.testgenerator.test.TestedBean\\\",\\\"libelle\\\":\\\"coucou\\\",\\\"value\\\":7,\\\"valueObject\\\":8,\\\"set\\\":{\\\"@type\\\":\\\"java.util.HashSet\\\"}}\")};" + "Object[] inputParams_exit = new Object[]{" + "JsonSerialisationUtils.deserialize(\"{\\\"@type\\\":\\\"double\\\",\\\"value\\\":45.2}\"), " + "JsonSerialisationUtils.deserialize(\"{\\\"@type\\\":\\\"com.freeyourcode.testgenerator.test.TestedBean\\\",\\\"libelle\\\":\\\"coucou\\\",\\\"value\\\":7,\\\"valueObject\\\":8,\\\"set\\\":{\\\"@type\\\":\\\"java.util.HashSet\\\"}}\")};" + "Object testedMethodResult = testedClass.myMethod2ParamPrimitifResultPrimitif((Double)inputParams_enter[0], (TestedBean)inputParams_enter[1]);" + "assertEquals(JsonSerialisationUtils.deserialize(\"{\\\"@type\\\":\\\"double\\\",\\\"value\\\":90.4}\"), testedMethodResult);" + "assertEquals(inputParams_exit, inputParams_enter);" + "}"); }
public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister, Serializable key) { if ( session.getEntityMode()==EntityMode.DOM4J ) { return new PersistentElementHolder(session, persister, key); } else { return new PersistentSet(session); } }
public PersistentCollection wrap(SessionImplementor session, Object collection) { if ( session.getEntityMode()==EntityMode.DOM4J ) { return new PersistentElementHolder( session, (Element) collection ); } else { return new PersistentSet( session, (java.util.Set) collection ); } }
/** * @see com.thoughtworks.xstream.converters.Converter#marshal(java.lang.Object, com.thoughtworks.xstream.io.HierarchicalStreamWriter, * com.thoughtworks.xstream.converters.MarshallingContext) */ public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { Object collection = source; if (source instanceof PersistentCollection) { PersistentCollection col = (PersistentCollection) source; col.forceInitialization(); // ToDo ES: collection = col.getCollectionSnapshot().getSnapshot(); collection = col.getStoredSnapshot(); } // the set is returned as a map by Hibernate (unclear why exactly) if (source instanceof PersistentSortedSet) { collection = new TreeSet(((HashMap) collection).values()); } else if (source instanceof PersistentSet) { // collection = new HashSet(((HashMap)collection).entrySet()); collection = new HashSet(((HashMap) collection).values()); } // delegate the collection to the approapriate converter if (listSetConverter.canConvert(collection.getClass())) { listSetConverter.marshal(collection, writer, context); return; } if (mapConverter.canConvert(collection.getClass())) { mapConverter.marshal(collection, writer, context); return; } if (treeMapConverter.canConvert(collection.getClass())) { treeMapConverter.marshal(collection, writer, context); return; } if (treeSetConverter.canConvert(collection.getClass())) { treeSetConverter.marshal(collection, writer, context); return; } defaultConverter.marshal(collection, writer, context); }
public void init() { collectionMap.put(PersistentBag.class, ArrayList.class); collectionMap.put(PersistentList.class, ArrayList.class); collectionMap.put(PersistentMap.class, HashMap.class); collectionMap.put(PersistentSet.class, HashSet.class); collectionMap.put(PersistentSortedMap.class, TreeMap.class); collectionMap.put(PersistentSortedSet.class, TreeSet.class); }
public void testWriteMethodDirtying() { Parent parent = new Parent( "p1" ); Child child = new Child( "c1" ); parent.getChildren().add( child ); child.setParent( parent ); Child otherChild = new Child( "c2" ); Session session = openSession(); session.beginTransaction(); session.save( parent ); session.flush(); // at this point, the set on parent has now been replaced with a PersistentSet... PersistentSet children = ( PersistentSet ) parent.getChildren(); assertFalse( children.add( child ) ); assertFalse( children.isDirty() ); assertFalse( children.remove( otherChild ) ); assertFalse( children.isDirty() ); HashSet otherSet = new HashSet(); otherSet.add( child ); assertFalse( children.addAll( otherSet ) ); assertFalse( children.isDirty() ); assertFalse( children.retainAll( otherSet ) ); assertFalse( children.isDirty() ); otherSet = new HashSet(); otherSet.add( otherChild ); assertFalse( children.removeAll( otherSet ) ); assertFalse( children.isDirty() ); assertTrue( children.retainAll( otherSet )); assertTrue( children.isDirty() ); assertTrue( children.isEmpty() ); children.clear(); session.delete( child ); assertTrue( children.isDirty() ); session.flush(); children.clear(); assertFalse( children.isDirty() ); session.delete( parent ); session.getTransaction().commit(); session.close(); }
/** * {@inheritDoc} */ @Override public void onCollectionUpdate(Object collection, Serializable key) { if (!(collection instanceof PersistentCollection)) { return; } PersistentCollection pc = (PersistentCollection) collection; Object owner = pc.getOwner(); if (!(processor.isAuditableEntity(owner))) { return; } if (audits.get() == null) { audits.set(new HashSet<AuditLogHelper>()); } String role = pc.getRole(); Serializable oldSerial = pc.getStoredSnapshot(); Object oldV = null; if (oldSerial != null && pc instanceof PersistentSet) { // PersistentSet seems to build a strange map where the key is also the value. oldV = ((Map) oldSerial).keySet(); } else { oldV = oldSerial; } Object newV = pc.getValue(); int idx = role.lastIndexOf('.'); String className = role.substring(0, idx); String property = role.substring(idx + 1); Map<String, String> tabColMA = getColumnTableName(className, property); AuditLogRecord record = getOrCreateRecord(owner, key, className, tabColMA.get(TABLE_NAME), AuditType.UPDATE); Getter getter; try { getter = getIdGetter(Class.forName(className)); } catch (ClassNotFoundException ex) { throw new HibernateException(ex); } AuditLogHelper helper = new AuditLogHelper(record, owner, getter); audits.get().add(helper); helper.getDetails().add(new DetailHelper(property, tabColMA.get(COLUMN_NAME), oldV, newV)); }
public Integer createUpdate(Integer entityId, MessageDTO dto) { set(dto.getTypeId(), dto.getLanguageId(), entityId); // it's just so easy to delete cascade and recreate ...:D if (messageRow != null) { messageDas.delete(messageRow); } messageRow = messageDas.create(dto.getTypeId(), entityId, dto .getLanguageId(), dto.getUseFlag()); // add the sections with the lines to the message entity for (int f = 0; f < dto.getContent().length; f++) { MessageSection section = dto.getContent()[f]; // create the section bean NotificationMessageSectionDTO sectionBean = messageSectionHome .create(section.getSection()); int index = 0; while (index < section.getContent().length()) { String line; if (index + MessageDTO.LINE_MAX.intValue() <= section .getContent().length()) { line = section.getContent().substring(index, index + MessageDTO.LINE_MAX.intValue()); } else { line = section.getContent().substring(index); } index += MessageDTO.LINE_MAX.intValue(); NotificationMessageLineDTO nml = messageLineHome.create(line); nml.setNotificationMessageSection(sectionBean); sectionBean.getNotificationMessageLines().add(nml); } sectionBean.setNotificationMessage(messageRow); messageRow.getNotificationMessageSections().add(sectionBean); } PersistentSet msjs = (PersistentSet) messageRow .getNotificationMessageSections(); NotificationMessageSectionDTO nnnn = ((NotificationMessageSectionDTO) msjs .toArray()[0]); PersistentSet nm = (PersistentSet) nnnn.getNotificationMessageLines(); messageRow.setIncludeAttachment(dto.getIncludeAttachment()); messageRow.setAttachmentType(dto.getAttachmentType()); messageRow.setAttachmentDesign(dto.getAttachmentDesign()); messageRow.setNotifyAdmin(dto.getNotifyAdmin()); messageRow.setNotifyPartner(dto.getNotifyPartner()); messageRow.setNotifyParent(dto.getNotifyParent()); messageRow.setNotifyAllParents(dto.getNotifyAllParents()); messageDas.save(messageRow); return messageRow.getId(); }