@Transactional(rollbackFor = Throwable.class)
  @Override
  public void saveOrUpdateRecords(ArchivedInferredLocationRecord... records) {
    List<ArchivedInferredLocationRecord> list =
        new ArrayList<ArchivedInferredLocationRecord>(records.length);
    for (ArchivedInferredLocationRecord record : records) list.add(record);
    _template.saveOrUpdateAll(list);

    // LastKnownRecord
    LinkedHashMap<Integer, CcAndInferredLocationRecord> lastKnownRecords =
        new LinkedHashMap<Integer, CcAndInferredLocationRecord>(records.length);
    for (ArchivedInferredLocationRecord record : records) {
      CcLocationReportRecord cc = findRealtimeRecord(record);
      if (cc != null) {
        CcAndInferredLocationRecord lastKnown = new CcAndInferredLocationRecord(record, cc);
        if (validationService.validateLastKnownRecord(lastKnown)) {
          lastKnownRecords.put(lastKnown.getVehicleId(), lastKnown);
        } else {
          discardRecord(lastKnown);
        }
      }
    }
    _template.saveOrUpdateAll(lastKnownRecords.values());
    _template.flush();
    _template.clear();
  }
  private ContentKey storeSimpleContent(String title) {

    ContentEntity content = factory.createContent("MyCategory", "en", "testuser", "0", new Date());
    ContentVersionEntity version = factory.createContentVersion("0", "testuser");

    ContentTypeConfig contentTypeConfig =
        ContentTypeConfigParser.parse(ContentHandlerName.CUSTOM, createSimpleContentTypeConfig());

    CustomContentData contentData = new CustomContentData(contentTypeConfig);

    TextDataEntryConfig titleConfig =
        new TextDataEntryConfig("myTitle", true, title, "contentdata/mytitle");
    contentData.add(new TextDataEntry(titleConfig, "relatedconfig"));

    version.setContentData(contentData);

    UserEntity runningUser = fixture.findUserByName("testuser");

    CreateContentCommand createContentCommand = new CreateContentCommand();
    createContentCommand.setCreator(runningUser);

    createContentCommand.populateCommandWithContentValues(content);
    createContentCommand.populateCommandWithContentVersionValues(version);

    createContentCommand.setBinaryDatas(new ArrayList<BinaryDataAndBinary>());
    createContentCommand.setUseCommandsBinaryDataToAdd(true);

    ContentKey contentKey = contentService.createContent(createContentCommand);

    hibernateTemplate.flush();
    hibernateTemplate.clear();

    return contentKey;
  }
Пример #3
0
 /* (non-Javadoc)
  * @see cn.cas.iue.dao.ConsumableDAO#updateConsumable(cn.cas.iue.bean.Consumable)
  */
 @Override
 public void updateConsumable(Consumable consumable) throws Exception {
   // TODO Auto-generated method stub
   try {
     hibernateTemplate.saveOrUpdate(consumable);
     hibernateTemplate.flush();
   } catch (RuntimeException re) {
     // TODO: handle exception
     throw re;
   } finally {
     hibernateTemplate.clear();
   }
 }
Пример #4
0
 // 出入库更新
 public void updateQuantity(Integer quantity, Integer consumableId) throws Exception {
   // TODO Auto-generated method stub
   try {
     hibernateTemplate.bulkUpdate(
         "update Consumable set quantity=? where consumableId=?",
         new Object[] {quantity, consumableId});
     hibernateTemplate.flush();
   } catch (RuntimeException re) {
     // TODO: handle exception
     throw re;
   } finally {
     hibernateTemplate.clear();
   }
 }
  @Transactional(rollbackFor = Throwable.class)
  @Override
  public void saveOrUpdateRecord(ArchivedInferredLocationRecord record) {
    _template.saveOrUpdate(record);

    CcLocationReportRecord cc = findRealtimeRecord(record);
    if (cc != null) {
      CcAndInferredLocationRecord lastKnown = new CcAndInferredLocationRecord(record, cc);
      _template.saveOrUpdate(lastKnown);
    }

    _template.flush();
    _template.clear();
  }
Пример #6
0
 /**
  * Create a HibernateTemplate from the SessionFactory and call flush() and clear() on it. Designed
  * to be used after "save" methods in tests: http://issues.appfuse.org/browse/APF-178.
  *
  * @throws org.springframework.beans.BeansException when can't find 'sessionFactory' bean
  */
 protected void flush() throws BeansException {
   HibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory);
   hibernateTemplate.flush();
   hibernateTemplate.clear();
 }
  @Before
  public void before() throws IOException, JDOMException {

    factory = fixture.getFactory();

    fixture.initSystemData();

    StringBuffer configXml = new StringBuffer();
    configXml.append("<config name=\"MyContentType\" version=\"1.0\">");
    configXml.append("     <form>");

    configXml.append("         <title name=\"myTitle\"/>");

    configXml.append("         <block name=\"General\">");

    configXml.append("             <input name=\"myTitle\" required=\"true\" type=\"text\">");
    configXml.append("                 <display>My title</display>");
    configXml.append("                 <xpath>contentdata/mytitle</xpath>");
    configXml.append("             </input>");

    configXml.append("         </block>");

    configXml.append("         <block name=\"Related content\">");

    configXml.append(
        "             <input name=\"myMultipleRelatedContent\" type=\"relatedcontent\" multiple=\"true\">");
    configXml.append("                 <display>My related content</display>");
    configXml.append("                 <xpath>contentdata/myrelatedcontents</xpath>");
    configXml.append("             </input>");

    configXml.append(
        "             <input name=\"mySoleRelatedContent\" type=\"relatedcontent\" multiple=\"false\">");
    configXml.append("                 <display>My sole related content</display>");
    configXml.append("                 <xpath>contentdata/mysolerelatedcontent</xpath>");
    configXml.append("                 <contenttype name=\"MyContentType\"/>");
    configXml.append("             </input>");

    configXml.append("         </block>");
    configXml.append("     </form>");
    configXml.append("</config>");
    configEl = JDOMUtil.parseDocument(configXml.toString()).getRootElement();
    config = XMLDocumentFactory.create(configXml.toString()).getAsJDOMDocument();

    fixture.createAndStoreNormalUserWithUserGroup("testuser", "testuser fullname", "testuserstore");
    hibernateTemplate.save(
        factory.createContentHandler(
            "Custom content", ContentHandlerName.CUSTOM.getHandlerClassShortName()));
    hibernateTemplate.save(
        factory.createContentType(
            "MyContentType", ContentHandlerName.CUSTOM.getHandlerClassShortName(), config));
    hibernateTemplate.save(factory.createUnit("MyUnit"));
    hibernateTemplate.save(
        factory.createCategory(
            "MyCategory", null, "MyContentType", "MyUnit", "testuser", "testuser"));
    hibernateTemplate.save(
        factory.createCategoryAccess(
            "MyCategory", fixture.findUserByName("testuser"), "read, create, approve"));

    hibernateTemplate.flush();
    hibernateTemplate.clear();

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr("127.0.0.1");
    ServletRequestAccessor.setRequest(request);
  }
  @Test
  public void testUpdateCurrentVersion() {
    ContentKey relatedContentKey1 = storeSimpleContent("rel1");
    ContentKey relatedContentKey2 = storeSimpleContent("rel2");
    ContentKey relatedContentKey3 = storeSimpleContent("rel3");
    ContentKey relatedContentKey4 = storeSimpleContent("rel4");
    ContentKey relatedContentKey5 = storeSimpleContent("rel5");

    ContentEntity content = factory.createContent("MyCategory", "en", "testuser", "0", new Date());
    ContentVersionEntity version = factory.createContentVersion("0", "testuser");

    ContentTypeConfig contentTypeConfig =
        ContentTypeConfigParser.parse(ContentHandlerName.CUSTOM, configEl);
    CustomContentData contentData = new CustomContentData(contentTypeConfig);
    TextDataEntryConfig titleConfig =
        new TextDataEntryConfig("myTitle", true, "Tittel", "contentdata/mytitle");
    contentData.add(new TextDataEntry(titleConfig, "test title"));

    RelatedContentDataEntryConfig multipleRelatedContentsConfig =
        (RelatedContentDataEntryConfig)
            contentTypeConfig.getInputConfig("myMultipleRelatedContent");

    contentData.add(
        new RelatedContentsDataEntry(multipleRelatedContentsConfig)
            .add(new RelatedContentDataEntry(multipleRelatedContentsConfig, relatedContentKey1))
            .add(new RelatedContentDataEntry(multipleRelatedContentsConfig, relatedContentKey2)));

    RelatedContentDataEntryConfig soleRelatedConfig =
        (RelatedContentDataEntryConfig) contentTypeConfig.getInputConfig("mySoleRelatedContent");

    contentData.add(new RelatedContentDataEntry(soleRelatedConfig, relatedContentKey3));

    version.setContentData(contentData);

    UserEntity runningUser = fixture.findUserByName("testuser");

    CreateContentCommand createContentCommand = new CreateContentCommand();
    createContentCommand.setCreator(runningUser);

    createContentCommand.populateCommandWithContentValues(content);
    createContentCommand.populateCommandWithContentVersionValues(version);

    createContentCommand.setBinaryDatas(new ArrayList<BinaryDataAndBinary>());
    createContentCommand.setUseCommandsBinaryDataToAdd(true);

    ContentKey contentKey = contentService.createContent(createContentCommand);

    hibernateTemplate.flush();
    hibernateTemplate.clear();

    ContentEntity persistedContent = contentDao.findByKey(contentKey);
    assertNotNull(persistedContent);

    ContentVersionEntity persistedVersion = persistedContent.getMainVersion();
    assertNotNull(persistedVersion);

    assertEquals(3, persistedVersion.getRelatedChildren(true).size());

    ContentEntity changedContent =
        factory.createContent("MyCategory", "en", "testuser", "0", new Date());
    changedContent.setKey(contentKey);
    ContentVersionEntity changedVersion = factory.createContentVersion("0", "testuser");
    changedVersion.setKey(persistedVersion.getKey());

    CustomContentData changedCD = new CustomContentData(contentTypeConfig);

    TextDataEntryConfig changedTitleConfig =
        new TextDataEntryConfig("myTitle", true, "Tittel", "contentdata/mytitle");
    changedCD.add(new TextDataEntry(changedTitleConfig, "changed title"));

    changedCD.add(
        new RelatedContentsDataEntry(multipleRelatedContentsConfig)
            .add(new RelatedContentDataEntry(multipleRelatedContentsConfig, relatedContentKey3))
            .add(new RelatedContentDataEntry(multipleRelatedContentsConfig, relatedContentKey5)));

    changedCD.add(new RelatedContentDataEntry(soleRelatedConfig, relatedContentKey4));

    changedVersion.setContentData(changedCD);

    UpdateContentCommand updateContentCommand =
        UpdateContentCommand.updateExistingVersion2(persistedVersion.getKey());
    updateContentCommand.setModifier(runningUser);
    updateContentCommand.setUpdateAsMainVersion(false);

    updateContentCommand.populateContentValuesFromContent(persistedContent);
    updateContentCommand.populateContentVersionValuesFromContentVersion(changedVersion);

    contentService.updateContent(updateContentCommand);

    hibernateTemplate.flush();
    hibernateTemplate.clear();

    ContentEntity contentAfterUpdate = contentDao.findByKey(contentKey);
    ContentVersionEntity versionAfterUpdate =
        contentVersionDao.findByKey(persistedVersion.getKey());

    Document contentDataXmlAfterUpdate = versionAfterUpdate.getContentDataAsJDomDocument();

    AssertTool.assertXPathEquals(
        "/contentdata/mysolerelatedcontent/@key",
        contentDataXmlAfterUpdate,
        relatedContentKey4.toString());
    AssertTool.assertXPathEquals(
        "/contentdata/myrelatedcontents/content[1]/@key",
        contentDataXmlAfterUpdate,
        relatedContentKey3.toString());
    AssertTool.assertXPathEquals(
        "/contentdata/myrelatedcontents/content[2]/@key",
        contentDataXmlAfterUpdate,
        relatedContentKey5.toString());

    assertEquals(3, versionAfterUpdate.getRelatedChildren(true).size());
  }