@Test
  public void addOneSideEntity_ClearCache_TestForPersistedEntitiesIfCachingIsWorking()
      throws Exception {
    RelationEntities.LazyOneSide oneSide = new RelationEntities.LazyOneSide("One");
    oneSideDao.create(oneSide);

    RelationEntities.LazyManySide manySide1 = new RelationEntities.LazyManySide("Many 1");
    RelationEntities.LazyManySide manySide2 = new RelationEntities.LazyManySide("Many 2");
    RelationEntities.LazyManySide manySide3 = new RelationEntities.LazyManySide("Many 3");

    oneSide.getManySides().add(manySide1);
    oneSide.getManySides().add(manySide2);
    oneSide.getManySides().add(manySide3);

    oneSideDao.clearObjectCache();
    manySideDao.clearObjectCache();

    RelationEntities.LazyOneSide persistedOneSide = oneSideDao.queryForId(oneSide.getId());

    int iteration = 0;
    while (iteration < 5) { // test if also after 5 iterations objects are the same
      RelationEntities.LazyOneSide newlyRetrievedOneSide = oneSideDao.queryForId(oneSide.getId());
      Assert.assertSame(persistedOneSide, newlyRetrievedOneSide);

      Assert.assertSame(
          persistedOneSide.getManySides().get(0), manySideDao.queryForId(manySide1.getId()));
      Assert.assertSame(
          persistedOneSide.getManySides().get(1), manySideDao.queryForId(manySide2.getId()));
      Assert.assertSame(
          persistedOneSide.getManySides().get(2), manySideDao.queryForId(manySide3.getId()));

      iteration++;
    }
  }
 public <E> List<E> retireveModulesMapping(String projectID) {
   List<PuppetModuleMapping> moduleMappingList = new ArrayList<PuppetModuleMapping>();
   try {
     ProjectBean project = projectDao.queryForId(Integer.parseInt(projectID));
     List<PuppetModuleBean> puppetModules =
         PuppetModuleDAOManager.getInstance().puppetModuleDao.queryForEq("isDisabled", false);
     List<MachineConfigurationBean> machineConfigs =
         MachineConfigDAOManager.getInstance().machineConfigDao.queryForEq("isDisabled", false);
     if (!(puppetModules.isEmpty())) {
       moduleMappingList =
           PuppetModuleDAOManager.getInstance()
               .puppetModuleMappingDao
               .queryBuilder()
               .where()
               .in(PuppetModuleMapping.MODULE_ID_FIELD_NAME, puppetModules)
               .and()
               .in(PuppetModuleMapping.MACHINE_ID_FIELD_NAME, machineConfigs)
               .and()
               .eq(ShellScriptMapping.PROJECT_ID_FIELD_NAME, project)
               .query();
     }
   } catch (SQLException e) {
     logger.error("Error in retireving module mapping: " + e.getMessage());
   }
   return (List<E>) moduleMappingList;
 }
 public void addClientUser(TwitterSession twitterSession, CreateCompletedListener addedListener) {
   ConnectionSource connectionSource = null;
   try {
     DataBaseHelper helper = new DataBaseHelper(Global.getInstance().getApplicationContext());
     connectionSource = helper.getConnectionSource();
     TableUtils.createTableIfNotExists(connectionSource, ClientUserTable.class);
     Dao<ClientUserTable, String> dao = helper.getDao(ClientUserTable.class);
     ClientUserTable table1 = dao.queryForId("" + twitterSession.getUserId());
     if (table1 == null) {
       ClientUserTable table = new ClientUserTable(twitterSession);
       dao.createOrUpdate(table);
       new ClientUser(
           clientUsers.size(),
           twitterSession,
           clientUser -> {
             clientUsers.add(clientUser);
             addedListener.completed(clientUser);
           },
           TwitterException::printStackTrace);
     } else {
       addedListener.completed(null);
     }
   } catch (SQLException e) {
     e.printStackTrace();
     throw new RuntimeException(e);
   } finally {
     if (connectionSource != null) {
       try {
         connectionSource.close();
       } catch (SQLException e) {
         e.printStackTrace();
       }
     }
   }
 }
 public Company getCompany(String id) throws Exception {
   Company company = dao.queryForId(id);
   if (company == null) {
     throw new RequestException("No company with id %s found", id);
   }
   return company;
 }
  @Test
  public void addOneSideEntity_ClearCacheQueryForAllThenGetEntity_EntityGottenIsNowCached()
      throws Exception {
    RelationEntities.LazyOneSide oneSide = new RelationEntities.LazyOneSide("One");
    oneSideDao.create(oneSide);

    RelationEntities.LazyManySide manySide1 = new RelationEntities.LazyManySide("Many 1");
    RelationEntities.LazyManySide manySide2 = new RelationEntities.LazyManySide("Many 2");
    RelationEntities.LazyManySide manySide3 = new RelationEntities.LazyManySide("Many 3");
    RelationEntities.LazyManySide manySide4 = new RelationEntities.LazyManySide("Many 4");
    RelationEntities.LazyManySide manySide5 = new RelationEntities.LazyManySide("Many 5");

    oneSide.getManySides().add(manySide1);
    oneSide.getManySides().add(manySide2);
    oneSide.getManySides().add(manySide3);
    oneSide.getManySides().add(manySide4);
    oneSide.getManySides().add(manySide5);

    oneSideDao.clearObjectCache();
    manySideDao.clearObjectCache();

    RelationEntities.LazyOneSide persistedOneSide = oneSideDao.queryForId(oneSide.getId());
    OpenLazyLoadingEntitiesCollection persistedManySides =
        (OpenLazyLoadingEntitiesCollection) persistedOneSide.getManySides();
    RelationEntities.LazyManySide anyEntity = persistedOneSide.getManySides().get(0);

    Assert.assertEquals(1, persistedManySides.getCachedEntities().size());
    Assert.assertTrue(persistedManySides.getCachedEntities().containsValue(anyEntity));
  }
  @Test
  public void
      addOneSideEntity_ClearCacheQueryForAll_LazyCollectionRetrievedIdsButEntitiesNotCachedYet()
          throws Exception {
    RelationEntities.LazyOneSide oneSide = new RelationEntities.LazyOneSide("One");
    oneSideDao.create(oneSide);

    RelationEntities.LazyManySide manySide1 = new RelationEntities.LazyManySide("Many 1");
    RelationEntities.LazyManySide manySide2 = new RelationEntities.LazyManySide("Many 2");
    RelationEntities.LazyManySide manySide3 = new RelationEntities.LazyManySide("Many 3");
    RelationEntities.LazyManySide manySide4 = new RelationEntities.LazyManySide("Many 4");
    RelationEntities.LazyManySide manySide5 = new RelationEntities.LazyManySide("Many 5");

    oneSide.getManySides().add(manySide1);
    oneSide.getManySides().add(manySide2);
    oneSide.getManySides().add(manySide3);
    oneSide.getManySides().add(manySide4);
    oneSide.getManySides().add(manySide5);

    oneSideDao.clearObjectCache();
    manySideDao.clearObjectCache();

    RelationEntities.LazyOneSide persistedOneSide = oneSideDao.queryForId(oneSide.getId());
    OpenLazyLoadingEntitiesCollection persistedManySides =
        (OpenLazyLoadingEntitiesCollection) persistedOneSide.getManySides();

    Assert.assertEquals(
        oneSide.getManySides().size(), persistedManySides.getRetrievedIndices().size());
    Assert.assertEquals(0, persistedManySides.getCachedEntities().size());
  }
 public <E> List<E> retireveScriptsMapping(String projectID) {
   List<ShellScriptMapping> scriptMappingList = new ArrayList<ShellScriptMapping>();
   try {
     ProjectBean project = projectDao.queryForId(Integer.parseInt(projectID));
     List<ShellScriptBean> shellScripts =
         ShellScriptDAOManager.getInstance().shellScriptDao.queryForEq("isDisabled", false);
     List<MachineConfigurationBean> machineConfigs =
         MachineConfigDAOManager.getInstance().machineConfigDao.queryForEq("isDisabled", false);
     if (!(shellScripts.isEmpty())) {
       scriptMappingList =
           ShellScriptDAOManager.getInstance()
               .shellScriptMappingDao
               .queryBuilder()
               .where()
               .in(ShellScriptMapping.SCRIPT_ID_FIELD_NAME, shellScripts)
               .and()
               .in(ShellScriptMapping.MACHINE_ID_FIELD_NAME, machineConfigs)
               .and()
               .eq(ShellScriptMapping.PROJECT_ID_FIELD_NAME, project)
               .query();
     }
     // scriptMappingList =
     // ShellScriptDAOManager.getInstance().shellScriptMappingDao.queryForAll();
   } catch (SQLException e) {
     logger.error("Error in retireving scripts mapping: " + e.getMessage());
   }
   return (List<E>) scriptMappingList;
 }
 public Notebook get(int id) {
   try {
     return notebookDaoOpe.queryForId(id);
   } catch (SQLException e) {
     e.printStackTrace();
   }
   return null;
 }
Exemple #9
0
 public Lecture getReference(int id) {
   try {
     Dao<Lecture, Integer> dao = getDao();
     return dao.queryForId(id);
   } catch (SQLException e) {
     throw new RuntimeException(e);
   }
 }
Exemple #10
0
  public synchronized City queryById(int cityID) {
    try {
      return mDao.queryForId(cityID);
    } catch (SQLException e) {
      e.printStackTrace();
    }

    return null;
  }
Exemple #11
0
  public Remind get(int id) {
    try {
      return remindDao.queryForId(id);
    } catch (SQLException e) {
      e.printStackTrace();
    }

    return null;
  }
 /**
  * 按照专辑id 来查询指定专辑
  *
  * @param id 专辑id
  * @return 指定id的专辑对象
  */
 public DiggerAlbum queryById(String id) {
   try {
     return mDiggerAlbumDao.queryForId(id);
   } catch (SQLException e) {
     e.printStackTrace();
     Logger.e(
         TAG, "queryById " + DiggerAlbum.class.getSimpleName() + " failure >>>" + e.getMessage());
   }
   return null;
 }
Exemple #13
0
  public void remove() throws SQLException {
    Scanner sc = new Scanner(System.in);
    String id = sc.next();
    Dao<Account, String> accountDao = connect();

    accountDao.delete(accountDao.queryForId(id));
    System.out.println("Enter some string to continue");
    sc.next();
    connectionSource.close();
  }
 @SuppressWarnings("unchecked")
 private News FindMatchingNews(String newsId) {
   News matchingNews = null;
   try {
     Dao dao = mDatabaseManager.getSimpleDao();
     matchingNews = (News) dao.queryForId(newsId);
   } catch (SQLException e) {
     Log.e(TAG, e.getSQLState(), e);
   }
   return matchingNews;
 }
  @Override
  public <T> T read(String projectID) {

    ProjectBean project = null;
    try {
      project = projectDao.queryForId(Integer.parseInt(projectID));
    } catch (SQLException e) {
      logger.error("Error querying the project from DB : " + e.getMessage());
    }
    return (T) project;
  }
 private KnouNoticeInfo setChangeKnouNoticeInfo(KnouNoticeInfo knouNoticeInfo) {
   try {
     Dao<KnouNoticeInfo, Long> knouNoticeInfoDao = this.getHelper().getKnouNoticeInfoDao();
     if (knouNoticeInfo == null) {
       throw new Exception("null 이 나오면 안된다구요 ");
     }
     int result = knouNoticeInfoDao.update(knouNoticeInfo);
     System.out.println("setChangeKnouNoticeInfo:" + result);
     knouNoticeInfo = knouNoticeInfoDao.queryForId(knouNoticeInfo._ID);
   } catch (Exception e) {
     e.printStackTrace();
   }
   return knouNoticeInfo;
 }
  @Test
  public void addManySideEntity_TestIfTableEntryCreated() throws Exception {
    RelationEntities.LazyOneSide oneSide = new RelationEntities.LazyOneSide("One");
    oneSideDao.create(oneSide);

    RelationEntities.LazyManySide manySide = new RelationEntities.LazyManySide("Many 1");
    oneSide.getManySides().add(manySide);

    manySideDao.clearObjectCache();
    RelationEntities.LazyManySide persistedEntity = manySideDao.queryForId(manySide.getId());

    Assert.assertNotNull(persistedEntity);
    Assert.assertNotSame(
        manySide, persistedEntity); // to ensure persistedEntity hasn't just been taken from cache
  }
  @Test
  public void removeManySideEntity_TestIfTableEntryHasBeenDeleted() throws Exception {
    RelationEntities.LazyOneSide oneSide = new RelationEntities.LazyOneSide("One");

    RelationEntities.LazyManySide manySide = new RelationEntities.LazyManySide("Many 1");
    oneSide.getManySides().add(manySide);
    oneSideDao.create(oneSide);

    Long manySideId = manySide.getId();
    oneSide.getManySides().remove(manySide);

    manySideDao.clearObjectCache();
    RelationEntities.LazyManySide persistedEntity = manySideDao.queryForId(manySideId);
    Assert.assertNull(persistedEntity);
  }
  @Override
  public void create(Movement movement, Account account) throws Exception {

    OrmLiteAccount ormLiteAccount = accountDao.queryForId(account.getNumber());

    if (account.canProcessMovement(movement)) {
      if (movement.getType() == MOVEMENT_TYPE.CREDIT) {
        ormLiteAccount.setValue(ormLiteAccount.getValue() + movement.getValue());
      } else {
        ormLiteAccount.setValue(ormLiteAccount.getValue() - movement.getValue());
      }
      accountDao.update(ormLiteAccount);

      OrmLiteMovement ormLiteMovement = new OrmLiteMovement();
      ormLiteMovement.setDate(new Date());
      ormLiteMovement.setValue(movement.getValue());
      ormLiteMovement.setType(movement.getType().toString());
      ormLiteMovement.setAccount(ormLiteAccount);
      movementDao.create(ormLiteMovement);
    }
  }
  public StatusBean delete(JsonNode projectData) {
    StatusBean statusBean = new StatusBean();
    Integer userID = Integer.parseInt(projectData.get("userID").getTextValue());
    Integer projectID = Integer.parseInt(projectData.get("projectID").getTextValue());
    String location = Utilities.getInstance().fetchActiveProjectDirectory(userID);

    try {
      ProjectBean project = projectDao.queryForId(projectID);
      project.setIsDisabled(true);
      projectDao.update(project);
      List<MachineConfigurationBean> machineConfigList =
          MachineConfigDAOManager.getInstance().retireveBoxesForProject(projectID.toString());

      for (MachineConfigurationBean machineConfig : machineConfigList) {
        MachineConfigDAOManager.getInstance().delete(machineConfig.getMachineID().toString());
        String vagrantCommand = "vagrant destroy " + machineConfig.getVagrantID();
        VagrantCommandProcessor shellProcessor = new VagrantCommandProcessor();
        try {
          shellProcessor.executeVagrantFile(
              location, vagrantCommand, userID, new VagrantOutputStream());
        } catch (IOException e) {
          statusBean.setStatusCode(1);
          statusBean.setStatusMessage("error in destroting box" + e.getMessage());
        } catch (InterruptedException e) {
          statusBean.setStatusCode(1);
          statusBean.setStatusMessage("error in destroting box" + e.getMessage());
        }
      }

    } catch (SQLException e) {
      logger.error("Error deleting a project : " + e.getMessage());
      statusBean.setStatusCode(1);
      statusBean.setStatusMessage("Error deleting  a project : " + e.getMessage());
      e.printStackTrace();
    }
    statusBean.setStatusCode(0);
    statusBean.setStatusMessage("Project deleted successfully");
    return statusBean;
  }
  public Resolution register() throws AjaxException, RegisterException {
    log.info("RegisterAction.register() - Creating new user / userview");

    //		Object nextPage = getContext().getRequest().getSession().getAttribute("nextPage");

    ConnectionSource conn = null;
    try {
      conn = EntityManager.getConnection();

      /** Check that the view allows creating users */
      Dao<View, String> vDao = DaoManager.createDao(conn, View.class);
      View view = vDao.queryForId(getView().getId().toString());

      if (!view.getCreateUser()) {
        log.info("    View does not allow creating new users");
        throw new AjaxException(this, "userView", "Create User Disabled");
      }

      Dao<UserView, String> uvDao = DaoManager.createDao(conn, UserView.class);
      Dao<User, String> uDao = DaoManager.createDao(conn, User.class);

      /**
       * Try to create a user and userview If this fails there is likely already a user with the
       * same name and account
       */
      try {
        log.info("    Creating new user in system: " + getAccount().getAlias());

        User user = new User();
        user.setAccount(getAccount());
        user.setEmail(getEmail());
        user.setName(getUsername());
        user.setPassword(getPassword());

        uDao.create(user);

        log.info("    Creating new userview in for: " + getView().getAlias());

        UserView userView = new UserView();
        userView.setUser(user);
        userView.setView(getView());

        uvDao.create(userView);

        getContext().setUser(user);
        getContext().setAccessLevel(MyActionBeanContext.ACCESS_LEVEL_PASSWORD);

        /** User exists but nextPage was not specified. Redirect to the manager page */
        if (getContext().getUser() != null && nextPage == null) {
          log.info("    User exists but nextpage is null - redirect to ManagerAction");
          RedirectResolution resolution =
              new RedirectResolution(ManagerAction.class).includeRequestParameters(true);
          return resolution;
        }

        log.info("    Login successful - redirect to " + nextPage);

        RedirectResolution resolution = new RedirectResolution(getNextPage());
        return resolution;

      } catch (SQLException x) {
        log.error("RegisterException", x);

        return new ErrorResolution(
            HttpServletResponse.SC_PRECONDITION_FAILED,
            "{\"result\":\"error\",\"message\":\"" + x.getMessage() + "\"}");
      }
    } catch (Exception x) {
      x.printStackTrace();

      log.error("RegisterException", x);

      getContext().getValidationErrors().add("entity", new SimpleError("create user error"));
      throw new RegisterException(this, getAccount(), getView(), "Exception encountered");
    } finally {
      try {
        conn.close();
      } catch (SQLException e) {
      }
    }
  }
 public RedmineRole fetchById(int id) throws SQLException {
   RedmineRole item;
   item = dao.queryForId(id);
   if (item == null) item = new RedmineRole();
   return item;
 }
 public RedmineAttachment fetchById(long id) throws SQLException {
   RedmineAttachment item = dao.queryForId(id);
   if (item == null) item = new RedmineAttachment();
   return item;
 }
 public RedmineWiki fetchById(long id) throws SQLException {
   RedmineWiki item = dao.queryForId(id);
   if (item == null) item = new RedmineWiki();
   return item;
 }