Search in sources :

Example 1 with Hanzi_data

use of com.reinard.learnhanzi.json.Hanzi_data in project learn-hanzi by reinardhz.

the class HanziServiceImpl method insertHanzi.

/**
 * A method to insert inputted hanzi to database.
 *
 * If the data successfully inserted to database , return the successfull inserted hanzi and its created date in json format.
 * Response json string example: {"hanzi_data":[{"hanzi":"會", "created_date":"1491448282654"}]}. <br/>
 *
 * <br/>
 *
 * @param hanzi - String hanzi to insert.
 * @return String result - The inserted hanzi data in json format, or String: "Error: Cannot insert. Data already exist.", if trying to insert the already inserted data.
 * @throws Exception - If errors occurs when inserting data to database.
 */
public String insertHanzi(String input) throws Exception {
    try {
        logger.info("Inserting hanzi to database...");
        logger.debug("preparing the data...");
        HanziData inputHanziData = new HanziData();
        inputHanziData.setHanzi(input);
        inputHanziData.setCreated_date(System.currentTimeMillis());
        logger.debug("preparing the child...");
        Set<UserAndHanzi> childs = new HashSet<UserAndHanzi>();
        UserAndHanzi child = new UserAndHanzi();
        UserData userData = new UserData();
        // manual input
        userData.setUser_id(1L);
        child.setUserData(userData);
        child.setHanziData(inputHanziData);
        childs.add(child);
        // add the child
        logger.debug("adding the child to the data...");
        inputHanziData.setUserAndHanzi(childs);
        logger.debug("call dao, to insert data to database");
        HanziData insertedHanziData = hanziDaoImpl.insert(inputHanziData);
        logger.info("Insert hanzi to database succeed");
        logger.info("Converting the inserted data to json object...");
        logger.debug("Preparing the json object...");
        HanziDataJsonResponseObject resultJsonObject = new HanziDataJsonResponseObject();
        logger.debug("Preparing the child json object...");
        Hanzi_data resultChildJsonObject = new Hanzi_data();
        resultChildJsonObject.setHanzi(insertedHanziData.getHanzi());
        resultChildJsonObject.setCreated_date(String.valueOf(insertedHanziData.getCreated_date()));
        logger.debug("Converting the child json object into array...");
        List<Hanzi_data> var = new ArrayList<>();
        var.add(resultChildJsonObject);
        Hanzi_data[] resultChildJsonArray = var.<Hanzi_data>toArray(new Hanzi_data[0]);
        logger.debug("Adding the array into json object...");
        resultJsonObject.setHanzi_data(resultChildJsonArray);
        logger.debug("Converting json object into json string...");
        ObjectMapper mapper = new ObjectMapper();
        String result = mapper.writeValueAsString(resultJsonObject);
        return result;
    } catch (PersistenceException pe) {
        logger.error("Cannot insert. Data already exist.");
        return "Error: Cannot insert. Data already exist.";
    } catch (Exception e) {
        logger.error("Unexpected error occurred when inserting hanzi to database.");
        throw e;
    }
}
Also used : Hanzi_data(com.reinard.learnhanzi.json.Hanzi_data) HanziDataJsonResponseObject(com.reinard.learnhanzi.json.HanziDataJsonResponseObject) UserData(com.reinard.learnhanzi.models.UserData) ArrayList(java.util.ArrayList) PersistenceException(javax.persistence.PersistenceException) UserAndHanzi(com.reinard.learnhanzi.models.UserAndHanzi) PersistenceException(javax.persistence.PersistenceException) HanziData(com.reinard.learnhanzi.models.HanziData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet)

Example 2 with Hanzi_data

use of com.reinard.learnhanzi.json.Hanzi_data in project learn-hanzi by reinardhz.

the class HanziServiceImpl method selectAll.

/**
 * Select all hanzi from "hanzi_data" table. <br/><br/>
 * Example of returned json: <br/>
 * {"hanzi_data":[{"hanzi":"我","created_date":"1491448282654"},{"hanzi":"你","created_date":"1491451859750"}]} <br/>
 *
 * @return String resultJson - All hanzi_data in json format, <i>null</i> if there is no data in database.
 * @throws Exception - If errors occurs when select all data from "hanzi_data" table.
 */
public String selectAll() throws Exception {
    try {
        List<HanziData> allHanzi = hanziDaoImpl.selectAll();
        if (allHanzi == null || allHanzi.isEmpty()) {
            return null;
        }
        // convert result to json array:
        List<Hanzi_data> hanziJsonList = new ArrayList<>();
        for (HanziData hanzi : allHanzi) {
            Hanzi_data hanziJson = new Hanzi_data();
            // hanziJson.setHanzi_id(String.valueOf(hanzi.getHanzi_id()));
            hanziJson.setHanzi(hanzi.getHanzi());
            hanziJson.setCreated_date(String.valueOf(hanzi.getCreated_date()));
            hanziJsonList.add(hanziJson);
        }
        /* //insert data to 2000 (delete this after testing)
			for(int i=1; i<3000; ++i){
				Hanzi_data hanziJson = new Hanzi_data();
				hanziJson.setHanzi("愛");
				hanziJson.setCreated_date(new Timestamp(System.currentTimeMillis()).toLocaleString());
				hanziJsonList.add(hanziJson);
			}*/
        Hanzi_data[] resultArray = hanziJsonList.<Hanzi_data>toArray(new Hanzi_data[0]);
        HanziDataJsonResponseObject resultJsonObject = new HanziDataJsonResponseObject();
        resultJsonObject.setHanzi_data(resultArray);
        // Convert to json
        ObjectMapper mapper = new ObjectMapper();
        String resultJson = mapper.writeValueAsString(resultJsonObject);
        return resultJson;
    } catch (Exception e) {
        logger.error("Error when trying to select all from \"hanzi_data\" table", e);
        throw e;
    }
}
Also used : Hanzi_data(com.reinard.learnhanzi.json.Hanzi_data) HanziDataJsonResponseObject(com.reinard.learnhanzi.json.HanziDataJsonResponseObject) ArrayList(java.util.ArrayList) HanziData(com.reinard.learnhanzi.models.HanziData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PersistenceException(javax.persistence.PersistenceException)

Example 3 with Hanzi_data

use of com.reinard.learnhanzi.json.Hanzi_data in project learn-hanzi by reinardhz.

the class TestController method test2.

// @RequestMapping(value = "/getAllHanzi", method = RequestMethod.GET)
// @ResponseBody
public String test2() throws Exception {
    Session newSession = hibernateSessionFactory.openSession();
    Transaction tx = null;
    try {
        tx = newSession.beginTransaction();
        List<HanziData> results = newSession.createCriteria(HanziData.class).list();
        tx.commit();
        List<Hanzi_data> hanzi_datas = new ArrayList<>();
        Hanzi_data hanzi_data = new Hanzi_data();
        for (HanziData curr : results) {
            // hanzi_data.setHanzi_id(String.valueOf(curr.getHanzi_id()));
            hanzi_data.setHanzi(curr.getHanzi());
            hanzi_datas.add(hanzi_data);
        }
        Hanzi_data[] resultHanziJson = hanzi_datas.<Hanzi_data>toArray(new Hanzi_data[0]);
        ObjectMapper mapper = new ObjectMapper();
        String resultJson = mapper.writeValueAsString(resultHanziJson);
        System.out.println(resultJson);
        return resultJson;
    } catch (Exception e) {
        if (tx != null)
            tx.rollback();
        return e.toString();
    } finally {
        if (newSession.isOpen())
            newSession.close();
    }
}
Also used : Hanzi_data(com.reinard.learnhanzi.json.Hanzi_data) Transaction(org.hibernate.Transaction) ArrayList(java.util.ArrayList) HanziData(com.reinard.learnhanzi.models.HanziData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Session(org.hibernate.Session)

Example 4 with Hanzi_data

use of com.reinard.learnhanzi.json.Hanzi_data in project learn-hanzi by reinardhz.

the class HanziServiceImpl method selectBy.

/**
 * Search unique hanzi in "hanzi_data" table. <br/><br/>
 *  Example of returned json: <br/>
 * {"hanzi_data":[{"hanzi":"我","created_date":"1491448282654"}]} <br/>
 *
 * @param hanzi - String hanzi to search.
 * @return String resultJson - A unique hanzi_data in json format, or <i>null</i> if the hanzi is not found.
 * @throws Exception - If errors occurs when search data from "hanzi_data" table.
 */
public String selectBy(String hanzi) throws Exception {
    try {
        HanziData hanziData = hanziDaoImpl.selectBy(hanzi);
        if (hanziData == null) {
            return null;
        }
        // prepare the child in json object:
        Hanzi_data hanziJson = new Hanzi_data();
        hanziJson.setHanzi(hanziData.getHanzi());
        hanziJson.setCreated_date(String.valueOf(hanziData.getCreated_date()));
        // convert child to array
        List<Hanzi_data> hanziJsonList = new ArrayList<>();
        hanziJsonList.add(hanziJson);
        Hanzi_data[] resultArray = hanziJsonList.<Hanzi_data>toArray(new Hanzi_data[0]);
        // prepare the json object
        HanziDataJsonResponseObject resultJsonObject = new HanziDataJsonResponseObject();
        resultJsonObject.setHanzi_data(resultArray);
        // Convert to json text
        ObjectMapper mapper = new ObjectMapper();
        String resultJson = mapper.writeValueAsString(resultJsonObject);
        return resultJson;
    } catch (Exception e) {
        logger.error("Exception when trying to select \"hanzi_data\" by inputted hanzi", e);
        e.printStackTrace(System.out);
        throw e;
    }
}
Also used : Hanzi_data(com.reinard.learnhanzi.json.Hanzi_data) HanziDataJsonResponseObject(com.reinard.learnhanzi.json.HanziDataJsonResponseObject) ArrayList(java.util.ArrayList) HanziData(com.reinard.learnhanzi.models.HanziData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PersistenceException(javax.persistence.PersistenceException)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 Hanzi_data (com.reinard.learnhanzi.json.Hanzi_data)4 HanziData (com.reinard.learnhanzi.models.HanziData)4 ArrayList (java.util.ArrayList)4 HanziDataJsonResponseObject (com.reinard.learnhanzi.json.HanziDataJsonResponseObject)3 PersistenceException (javax.persistence.PersistenceException)3 UserAndHanzi (com.reinard.learnhanzi.models.UserAndHanzi)1 UserData (com.reinard.learnhanzi.models.UserData)1 HashSet (java.util.HashSet)1 Session (org.hibernate.Session)1 Transaction (org.hibernate.Transaction)1