Package org.opentides.bean

Examples of org.opentides.bean.SystemCodes


  @Transactional
  public void evolve() {

    // get current db version
    SystemCodes version;
    try {
        version = systemCodesDAO.loadBySystemCodesByKey("DB_VERSION");
    } catch (NoResultException nre) {
        version = null;
    }
    if (version==null) {
      // no version available yet, lets create one
      version = new SystemCodes();
      version.setSkipAudit(true);
      version.setKey("DB_VERSION");
      version.setNumberValue(0l);
      systemCodesDAO.saveEntityModel(version);
      // initialize default admin user
      userService.setupAdminUser();
    }

    // skip evolve if there is nothing in the evolve list
    if (evolveList.isEmpty()) {
      _log.info("No evolve scripts found.");
      return;
    }
   
    // sort the evolve list
    Collections.sort(evolveList, new VersionComparator());
    // check for duplicate version numbers
    for (int i=0; i<(evolveList.size()-1); i++) {
      if (evolveList.get(i).getVersion() == evolveList.get(i+1).getVersion()) {
        // we have a duplicate version... exit
        throw new InvalidImplementationException(
            "Duplicate version number ["+evolveList.get(i).getVersion() +
            "] detected on evolve script for "+evolveList.get(i).getClass().getName() +
            " and " + evolveList.get(i+1).getClass().getName());
      }
    }
   
    // get number of latest evolve script
    long currVersion   = version.getNumberValue();
    long latestVersion = evolveList.get(evolveList.size()-1).getVersion();
   
    if (currVersion>=latestVersion) {
      _log.info("Database is already at version " + currVersion);
      return;
    } else {
      _log.info("Updating database from version " + currVersion +" to version " + latestVersion );
    }
   
    // execute new evolve scripts
    for (DBEvolve evolve:evolveList) {
      if (evolve.getVersion() > currVersion) {
        // let's execute this evolve script
        _log.info("Executing evolve version ["+evolve.getVersion()+"] - "+evolve.getDescription());
        evolve.execute();
        // if successful, update current db version
        version.setNumberValue(Long.valueOf(evolve.getVersion()));
        version.setSkipAudit(true);
        systemCodesDAO.saveEntityModel(version);
        _log.info("Success.");
      }
    }
    // as precaution let's update db version again
    version.setNumberValue(Long.valueOf(latestVersion));
    version.setSkipAudit(true);
    systemCodesDAO.saveEntityModel(version);
   
    _log.info("Database is now updated to version "+latestVersion);
  }
View Full Code Here


  /**
   * Retrieves SystemCodes for the given category
   */
  public List<SystemCodes> findSystemCodesByCategory(String category) {   
        SystemCodes example = new SystemCodes();
        example.setCategory(category);
        return findByExample(example, true);       
  }
View Full Code Here

   * Increments value of numberValue by used.
   * Useful for custom generation of id.
   */
  public Long incrementValue(String key) {
      synchronized(SystemCodes.class) {
        SystemCodes code = loadBySystemCodesByKey(key);
        if (code==null) code = new SystemCodes(key);
            code.setSkipAudit(true);    // no need to audit auto-generated keys
        code.incrementNumberValue();
        code.setCategory("KEYGEN");
        this.saveEntityModel(code);
        return code.getNumberValue();
      }
  }
View Full Code Here

  @Test
  public void testLogEvent(){
    int prevCount = jdbcTemplate.queryForInt("SELECT count(*) FROM HISTORY_LOG");
   
    jdbcTemplate.execute("INSERT INTO SYSTEM_CODES(CATEGORY_,KEY_,VALUE_) VALUES('OFFICE','HR','Human Resources')");
    SystemCodes sc = (SystemCodes) jdbcTemplate.query("SELECT * FROM SYSTEM_CODES WHERE VALUE_='Human Resources'", new SystemCodesExtractor());
   
    AuditLogDAOImpl.logEvent("Short","Sample log for testing.", sc);
   
    int currCount = jdbcTemplate.queryForInt("SELECT count(*) FROM HISTORY_LOG");
    assertEquals(prevCount+1, currCount);
View Full Code Here

 
  private static final class SystemCodesExtractor implements ResultSetExtractor{
    public Object extractData(ResultSet rs) throws SQLException,
        DataAccessException {
      rs.next();
      SystemCodes sc = new SystemCodes();
      sc.setId(rs.getLong("id"));
      sc.setCategory(rs.getString("category_"));
      sc.setKey(rs.getString("key_"));
          sc.setValue(rs.getString("value_"));
          return sc;
    }
View Full Code Here

    int actual = dao.findSystemCodesByCategory("COUNTRY").size();
    assertEquals(expected, actual);
  }
 
  public void testLoadBySystemCodesByKey(){
    SystemCodes expected = (SystemCodes) jdbcTemplate.query("SELECT * FROM SYSTEM_CODES WHERE KEY_='AD'", new SystemCodesExtractor());   
    SystemCodes actual = dao.loadBySystemCodesByKey("AD");
   
    assertEquals(expected.getId(), actual.getId());
    assertEquals(expected.getCategory(),actual.getCategory());
    assertEquals(expected.getValue(), actual.getValue());
  }
View Full Code Here

 
  private SystemCodesDAO dao;

  private static final class SystemCodesMapper implements RowMapper {
      public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
          SystemCodes sc = new SystemCodes();
          sc.setCategory(rs.getString("category_"));
          sc.setKey(rs.getString("key_"));
          sc.setValue(rs.getString("value_"));
          return sc;
      }
View Full Code Here

 
  private static final class SystemCodesExtractor implements ResultSetExtractor{
    public Object extractData(ResultSet rs) throws SQLException,
        DataAccessException {
      rs.next();
      SystemCodes sc = new SystemCodes();
      sc.setId(rs.getLong("id"));
      sc.setCategory(rs.getString("category_"));
      sc.setKey(rs.getString("key_"));
          sc.setValue(rs.getString("value_"));
          return sc;
    }
View Full Code Here

    } catch (InvalidImplementationException iie) {
    }
  }
 
  public void testLoadEntityModel() {
    SystemCodes sc1 = dao.loadEntityModel(9001l);
    assertEquals("PH", sc1.getKey());
    assertEquals("Philippines", sc1.getValue());
    assertEquals("COUNTRY", sc1.getCategory());
    //TODO: How to test with lock functionality
  }
View Full Code Here

  }
 
  public void testSaveEntityModelAddObject() {
    int count = jdbcTemplate.queryForInt("select count(*) from SYSTEM_CODES where KEY_='AU' and CATEGORY_='COUNTRY'");
    assertEquals(0, count);
    SystemCodes sc1 = new SystemCodes();
    sc1.setKey("AU");
    sc1.setCategory("COUNTRY");
    sc1.setValue("Australia");
    dao.saveEntityModel(sc1);
    count = jdbcTemplate.queryForInt("select count(*) from SYSTEM_CODES where KEY_='AU' and CATEGORY_='COUNTRY'");
    assertEquals(1, count);
  }
View Full Code Here

TOP

Related Classes of org.opentides.bean.SystemCodes

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.