Package com.activesession.exceptions

Examples of com.activesession.exceptions.ActiveSessionException


 
  public <T> List<T> findByCriateria(String query, Object[] params, Class<T> entityClass) {
    List<T> list = new ArrayList<T>();
   
    if (StringUtils.isBlank(query)) {
      throw new ActiveSessionException(ErrorMessageEnum.INVALID_QUERY);
    }
   
    for (int i = 0; i < params.length; i++) {
      int paramNumber = i + 1;
      String replace = "?" + paramNumber; 
      query =  StringUtils.replace(query, replace, params[i].toString());
    }
   
    HashMap<String, String> methodNameAndFieldName = new HashMap<String, String>();
    HashMap<String, String> fieldNameAndColumnName = new HashMap<String, String>();
   
    List<String> getMethods = new ArrayList<String>();
    ArrayList<String> columnNames = new ArrayList<String>();
   
    StringBuilder columns = new StringBuilder();
    try {
      prepareteState = connect.prepareStatement(query);
      resultSet = prepareteState.executeQuery();
     
      while (resultSet.next()) {
        T entity = entityClass.newInstance();
       
        Field fields[] = entityClass.getDeclaredFields();   //getting all declared fields in entity class into array
        initializeMethodNameAndFieldName(fields, methodNameAndFieldName, getMethods);
       
        Method[] methods = getEntityMethods(entity);
        initilizeColumnNamesAndFieldNamesAndColumnNames(methods, getMethods, columnNames, columns, methodNameAndFieldName, fieldNameAndColumnName);
       
        if (columns.length() == 0) {
          throw new ActiveSessionException(ErrorMessageEnum.ILLIGAL_COLUMN_ANNOTATION_EXCEPTION);
        }
     
        HashMap<String, Object> data = initializeData(resultSet, columnNames);
       
        entity = initilizeFieldsOfEntityObject(fields, entityClass, entity, data, fieldNameAndColumnName);
       
        list.add(entity);
      }
     
    } catch (Exception e) {
      throw new ActiveSessionException(e.getMessage(), e);
    }
   
    return list;
  }
View Full Code Here


    List<T> objects = findByCriateria(query, params, entityClass);
   
    if (objects.isEmpty()) {
      return null;
    } else if (objects.size() > 1) {
      throw new ActiveSessionException("There is more than one result! Check your data/query or use findByCriateria for more than one result");
    } else {
      return objects.get(0);
    }
  }
View Full Code Here

    // implementation of update query
    @Override
    public String makeUpdateQuery(List<String> listOfColumnNames, List<Object> listOfParameters, String tableName) {
        if (StringUtils.isBlank(tableName) || CollectionUtils.isBlank(listOfColumnNames) || CollectionUtils.isBlank(listOfParameters)) {
            if(listOfColumnNames.size() != listOfParameters.size()) {
                throw new ActiveSessionException("Update Query exception");
            }
        }

        StringBuilder idBuilder = new StringBuilder();
        StringBuilder parametersBuilder = new StringBuilder();
View Full Code Here

            Method[] methods = getEntityMethods(entityObject);
            initilizeColumnNamesAndFieldNamesAndColumnNames(methods, getMethods, columnNames, columns, methodNameAndFieldName, fieldNameAndColumnName);

            if (columns.length() == 0) {
                throw new ActiveSessionException(ErrorMessageEnum.ILLIGAL_COLUMN_ANNOTATION_EXCEPTION);
            }

            String query = queryMaker.makeFindQuery(columns.toString(), tableName, primaryKey);
            prepareteState = connect.prepareStatement(query);
            resultSet = prepareteState.executeQuery();

            HashMap<String, Object> data = initializeData(resultSet, columnNames);
            entityObject = initilizeFieldsOfEntityObject(fields, entityClass, entityObject, data, fieldNameAndColumnName);
        } catch (Exception e) {
            throw new ActiveSessionException(e.getMessage(), e);
        }

        return entityObject;
    }
View Full Code Here

        List<T> listOfAll = null;
        try {
            T entity = entityClass.newInstance();
            String tableName = getEntityTableName(entity);
            if (StringUtils.isBlank(tableName)) {
                throw new ActiveSessionException(ErrorMessageEnum.ILLIGAL_TABLE_ANNOTTAION_EXCEPTION);
            }

            listOfAll = new ArrayList<T>();
            String query = queryMaker.makeMaxQuery(tableName) ;

            prepareteState = connect.prepareStatement(query);
            resultSet = prepareteState.executeQuery();

            int lastId = 0;
            while(resultSet.next()){
                lastId  = resultSet.getInt(1);
            }

            if(lastId == 0){
                return listOfAll;
            }

            for (int i = 1; i <= lastId; i++) {
                T object = find(i, entityClass);
                if (object != null) {
                    listOfAll.add(object);
                }
            }
        } catch (Exception e) {
            throw new ActiveSessionException(e.getMessage(), e);
        } finally {
            closeStatements();
        }

        return listOfAll;
View Full Code Here

            }

            String query = queryMaker.makeDeleteQuery(tableName, id);
            NativeQueryExecutor(query);
        } catch (Exception e) {
            throw new ActiveSessionException(e.getMessage(), e);
        } finally {
            closeStatements();
        }
    }
View Full Code Here

            Method[] methods = getEntityMethods(entity); // all defined methods

            ArrayList<String> columnNames = new ArrayList<String>();
            addColumnNames(methods, columnNames); // get all column names from all methods
            if (columnNames.isEmpty()) {
                throw new ActiveSessionException(ErrorMessageEnum.NULL_COLUMN_EXCEPTION);
            }

            List<Object> parameters = new ArrayList<Object>(); // contains all attributes of entity object
            getMethodParamsAndAddThem(parameters, methods, entity); // get all methods and parameters and add them

            String query = makeQuery(columnNames, parameters, tableName); // create query
            if (StringUtils.isBlank(query)) {
                throw new ActiveSessionException("Query can`t be null or empty");
            }

            NativeQueryExecutor(query); // execute created query
        } catch (Exception e) {
            throw new ActiveSessionException(e.getMessage(), e);
        finally {
            closeStatements();
        }
    }
View Full Code Here

    public void flush() {
        try {
            connect.commit();
        } catch (SQLException e) {
            rollback();
            throw new ActiveSessionException(e.getMessage(), e);
        }
    }
View Full Code Here

  // roll back if something goes wrong
  protected void rollback() {
    try {
      connect.rollback();
    } catch (SQLException e) {
      throw new ActiveSessionException(e.getMessage(), e);
    }
  }
View Full Code Here

      try {
        prepareteState = connect.prepareStatement(query);
        prepareteState.execute();
      } catch (SQLException e) {
        rollback();
        throw new ActiveSessionException(e.getMessage(), e);
      }
    }
  }
View Full Code Here

TOP

Related Classes of com.activesession.exceptions.ActiveSessionException

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.