@Test public void testPersist() { final TestAbstractComponent grandparent = createAbstractComponent("grandparent", "0"); final TestAbstractComponent parent = createAbstractComponent("parent", "1"); final TestAbstractComponent child = createAbstractComponent("child", "2"); List<AbstractComponent> comps = Arrays.<AbstractComponent>asList(grandparent,parent,child); grandparent.addDelegateComponent(parent); parent.addDelegateComponent(child); for (AbstractComponent ac:comps) { Assert.assertTrue(ac.isDirty()); Assert.assertNull(ac.getCreationDate()); } serviceImpl.persist(Arrays.<AbstractComponent>asList(grandparent,parent,child)); for (AbstractComponent ac:comps) { Assert.assertFalse(ac.isDirty()); Assert.assertNotNull(ac.getCreationDate()); } TestAbstractComponent persistedGrandparent = (TestAbstractComponent) serviceImpl.getComponent(grandparent.getComponentId()); Assert.assertEquals(persistedGrandparent.getDisplayName(), grandparent.getDisplayName()); Assert.assertEquals(persistedGrandparent.getComponents().size(), 1, persistedGrandparent.getComponents().toString()); Assert.assertEquals(persistedGrandparent.getModelValue(), grandparent.getDisplayName()); Assert.assertEquals(persistedGrandparent.getComponents().iterator().next().getComponentId(), parent.getComponentId()); TestAbstractComponent persistedParent = (TestAbstractComponent) serviceImpl.getComponent(parent.getComponentId()); Assert.assertEquals(persistedParent.getDisplayName(), parent.getDisplayName()); Assert.assertEquals(persistedParent.getComponents().size(), 1); Assert.assertEquals(persistedParent.getDisplayName(), persistedParent.getDisplayName()); Assert.assertEquals(persistedParent.getComponents().iterator().next().getComponentId(), child.getComponentId()); }
@Override public void persist(Collection<AbstractComponent> componentsToPersist) { EntityManager em = entityManagerFactory.createEntityManager(); lastModified = lastPollTime != null ? lastPollTime.getAdjustedNow() : getCurrentTimeFromDatabase(); if (lastModified == null) { lastModified = new Date(); } try { em.getTransaction().begin(); for (AbstractComponent nc : componentsToPersist) { if (nc.getCreationDate() == null) { ComponentSpec cs = new ComponentSpec(); updateComponentSpec(nc, cs, em, false); em.persist(cs); } } for (AbstractComponent c : componentsToPersist) { updateComponentSpec(c, em.find(ComponentSpec.class, c.getComponentId()), em, true); } em.flush(); em.getTransaction().commit(); for (AbstractComponent c : componentsToPersist) { ComponentInitializer ci = c.getCapability(ComponentInitializer.class); ci.componentSaved(); if (c.getCreationDate() == null) { ci.setCreationDate(em.find(ComponentSpec.class, c.getComponentId()).getDateCreated()); } c.getCapability(Updatable.class).setVersion(em.find(ComponentSpec.class, c.getComponentId()).getObjVersion()); c.componentSaved(); List<WeakReference<AbstractComponent>> list = cache.get(c.getComponentId()); if (list == null) { list = Collections.synchronizedList(new LinkedList<WeakReference<AbstractComponent>>()); cache.put(c.getComponentId(), list); } list.add(new WeakReference<AbstractComponent>(c)); } } catch(OptimisticLockException ole) { throw new gov.nasa.arc.mct.api.persistence.OptimisticLockException(ole); } finally { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } em.close(); } }
PersistenceServiceImpl implements PersistenceProvider { @Override public void persist(Collection<AbstractComponent> componentsToPersist) { EntityManager em = entityManagerFactory.createEntityManager(); lastModified = lastPollTime != null ? lastPollTime.getAdjustedNow() : getCurrentTimeFromDatabase(); if (lastModified == null) { lastModified = new Date(); } try { em.getTransaction().begin(); for (AbstractComponent nc : componentsToPersist) { if (nc.getCreationDate() == null) { ComponentSpec cs = new ComponentSpec(); updateComponentSpec(nc, cs, em, false); em.persist(cs); } } for (AbstractComponent c : componentsToPersist) { updateComponentSpec(c, em.find(ComponentSpec.class, c.getComponentId()), em, true); } em.flush(); em.getTransaction().commit(); for (AbstractComponent c : componentsToPersist) { ComponentInitializer ci = c.getCapability(ComponentInitializer.class); ci.componentSaved(); if (c.getCreationDate() == null) { ci.setCreationDate(em.find(ComponentSpec.class, c.getComponentId()).getDateCreated()); } c.getCapability(Updatable.class).setVersion(em.find(ComponentSpec.class, c.getComponentId()).getObjVersion()); c.componentSaved(); List<WeakReference<AbstractComponent>> list = cache.get(c.getComponentId()); if (list == null) { list = Collections.synchronizedList(new LinkedList<WeakReference<AbstractComponent>>()); cache.put(c.getComponentId(), list); } list.add(new WeakReference<AbstractComponent>(c)); } } catch(OptimisticLockException ole) { throw new gov.nasa.arc.mct.api.persistence.OptimisticLockException(ole); } finally { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } em.close(); } } }
PersistenceServiceImpl implements PersistenceProvider { @Override public void persist(Collection<AbstractComponent> componentsToPersist) { EntityManager em = entityManagerFactory.createEntityManager(); lastModified = lastPollTime != null ? lastPollTime.getAdjustedNow() : getCurrentTimeFromDatabase(); if (lastModified == null) { lastModified = new Date(); } try { em.getTransaction().begin(); for (AbstractComponent nc : componentsToPersist) { if (nc.getCreationDate() == null) { ComponentSpec cs = new ComponentSpec(); updateComponentSpec(nc, cs, em, false); em.persist(cs); } } for (AbstractComponent c : componentsToPersist) { updateComponentSpec(c, em.find(ComponentSpec.class, c.getComponentId()), em, true); } em.flush(); em.getTransaction().commit(); for (AbstractComponent c : componentsToPersist) { ComponentInitializer ci = c.getCapability(ComponentInitializer.class); ci.componentSaved(); if (c.getCreationDate() == null) { ci.setCreationDate(em.find(ComponentSpec.class, c.getComponentId()).getDateCreated()); } c.getCapability(Updatable.class).setVersion(em.find(ComponentSpec.class, c.getComponentId()).getObjVersion()); c.componentSaved(); List<WeakReference<AbstractComponent>> list = cache.get(c.getComponentId()); if (list == null) { list = Collections.synchronizedList(new LinkedList<WeakReference<AbstractComponent>>()); cache.put(c.getComponentId(), list); } list.add(new WeakReference<AbstractComponent>(c)); } } catch(OptimisticLockException ole) { throw new gov.nasa.arc.mct.api.persistence.OptimisticLockException(ole); } finally { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } em.close(); } }  }
PersistenceServiceImpl implements PersistenceProvider { @Override public void persist(Collection<AbstractComponent> componentsToPersist) { EntityManager em = entityManagerFactory.createEntityManager(); lastModified = lastPollTime != null ? lastPollTime.getAdjustedNow() : getCurrentTimeFromDatabase(); if (lastModified == null) { lastModified = new Date(); } try { em.getTransaction().begin(); for (AbstractComponent nc : componentsToPersist) { if (nc.getCreationDate() == null) { ComponentSpec cs = new ComponentSpec(); updateComponentSpec(nc, cs, em, false); em.persist(cs); } } for (AbstractComponent c : componentsToPersist) { updateComponentSpec(c, em.find(ComponentSpec.class, c.getComponentId()), em, true); } em.flush(); em.getTransaction().commit(); for (AbstractComponent c : componentsToPersist) { ComponentInitializer ci = c.getCapability(ComponentInitializer.class); ci.componentSaved(); if (c.getCreationDate() == null) { ci.setCreationDate(em.find(ComponentSpec.class, c.getComponentId()).getDateCreated()); } c.getCapability(Updatable.class).setVersion(em.find(ComponentSpec.class, c.getComponentId()).getObjVersion()); c.componentSaved(); List<WeakReference<AbstractComponent>> list = cache.get(c.getComponentId()); if (list == null) { list = Collections.synchronizedList(new LinkedList<WeakReference<AbstractComponent>>()); cache.put(c.getComponentId(), list); } list.add(new WeakReference<AbstractComponent>(c)); } } catch(OptimisticLockException ole) { throw new gov.nasa.arc.mct.api.persistence.OptimisticLockException(ole); } finally { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } em.close(); } }  void bind(Platform platform); void unbind(Platform platform); void setEntityManagerProperties(Properties p); void activate(ComponentContext context); @Override AbstractComponent getComponentFromStore(String componentId); @Override AbstractComponent getComponent(String componentId); @Override Collection<AbstractComponent> getReferences(AbstractComponent component); @Override void startRelatedOperations(); @Override void completeRelatedOperations(boolean save); @Override void addComponentToWorkUnit(AbstractComponent component); Collection<AbstractComponent> getCurrentComponents(); @Override Set<String> getAllUsers(); @Override Collection<String> getUsersInGroup(String group); @Override void persist(Collection<AbstractComponent> componentsToPersist); @Override void delete(Collection<AbstractComponent> componentsToDelete); @Override boolean hasComponentsTaggedBy(String tagId); @Override List<AbstractComponent> getReferencedComponents(
			AbstractComponent component); @Override User getUser(String userId); @SuppressWarnings("unchecked") QueryResult findComponentsByBaseDisplayedNamePattern(String pattern, Properties props); @Override T getComponent(String externalKey, Class<T> componentType); @Override void updateComponentsFromDatabase(); @Override List<AbstractComponent> getBootstrapComponents(); @Override void addNewUser(String userId, String groupId, AbstractComponent mysandbox, AbstractComponent dropbox); @Override Map<String, ExtendedProperties> getAllProperties(String componentId); @Override AbstractComponent getComponent(String externalKey,
			String componentType); @Override void tagComponents(String tag,
			Collection<AbstractComponent> components); }
PersistenceServiceImpl implements PersistenceProvider { @Override public void persist(Collection<AbstractComponent> componentsToPersist) { EntityManager em = entityManagerFactory.createEntityManager(); lastModified = lastPollTime != null ? lastPollTime.getAdjustedNow() : getCurrentTimeFromDatabase(); if (lastModified == null) { lastModified = new Date(); } try { em.getTransaction().begin(); for (AbstractComponent nc : componentsToPersist) { if (nc.getCreationDate() == null) { ComponentSpec cs = new ComponentSpec(); updateComponentSpec(nc, cs, em, false); em.persist(cs); } } for (AbstractComponent c : componentsToPersist) { updateComponentSpec(c, em.find(ComponentSpec.class, c.getComponentId()), em, true); } em.flush(); em.getTransaction().commit(); for (AbstractComponent c : componentsToPersist) { ComponentInitializer ci = c.getCapability(ComponentInitializer.class); ci.componentSaved(); if (c.getCreationDate() == null) { ci.setCreationDate(em.find(ComponentSpec.class, c.getComponentId()).getDateCreated()); } c.getCapability(Updatable.class).setVersion(em.find(ComponentSpec.class, c.getComponentId()).getObjVersion()); c.componentSaved(); List<WeakReference<AbstractComponent>> list = cache.get(c.getComponentId()); if (list == null) { list = Collections.synchronizedList(new LinkedList<WeakReference<AbstractComponent>>()); cache.put(c.getComponentId(), list); } list.add(new WeakReference<AbstractComponent>(c)); } } catch(OptimisticLockException ole) { throw new gov.nasa.arc.mct.api.persistence.OptimisticLockException(ole); } finally { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } em.close(); } }  void bind(Platform platform); void unbind(Platform platform); void setEntityManagerProperties(Properties p); void activate(ComponentContext context); @Override AbstractComponent getComponentFromStore(String componentId); @Override AbstractComponent getComponent(String componentId); @Override Collection<AbstractComponent> getReferences(AbstractComponent component); @Override void startRelatedOperations(); @Override void completeRelatedOperations(boolean save); @Override void addComponentToWorkUnit(AbstractComponent component); Collection<AbstractComponent> getCurrentComponents(); @Override Set<String> getAllUsers(); @Override Collection<String> getUsersInGroup(String group); @Override void persist(Collection<AbstractComponent> componentsToPersist); @Override void delete(Collection<AbstractComponent> componentsToDelete); @Override boolean hasComponentsTaggedBy(String tagId); @Override List<AbstractComponent> getReferencedComponents(
			AbstractComponent component); @Override User getUser(String userId); @SuppressWarnings("unchecked") QueryResult findComponentsByBaseDisplayedNamePattern(String pattern, Properties props); @Override T getComponent(String externalKey, Class<T> componentType); @Override void updateComponentsFromDatabase(); @Override List<AbstractComponent> getBootstrapComponents(); @Override void addNewUser(String userId, String groupId, AbstractComponent mysandbox, AbstractComponent dropbox); @Override Map<String, ExtendedProperties> getAllProperties(String componentId); @Override AbstractComponent getComponent(String externalKey,
			String componentType); @Override void tagComponents(String tag,
			Collection<AbstractComponent> components);  }