Search in sources :

Example 1 with UserData

use of com.reinard.learnhanzi.models.UserData in project learn-hanzi by reinardhz.

the class HanziDaoImplTest method testInsert2.

// Case 2: Insert the already existing data in database
@Test(expected = javax.persistence.PersistenceException.class)
public void testInsert2() throws Exception {
    logger.debug("Start test");
    HanziData hanziData = new HanziData();
    hanziData.setHanzi("我");
    hanziData.setCreated_date(System.currentTimeMillis());
    // prepare the child
    Set<UserAndHanzi> childs = new HashSet<UserAndHanzi>();
    UserAndHanzi child = new UserAndHanzi();
    UserData userData = new UserData();
    userData.setUser_id(1L);
    child.setUserData(userData);
    child.setHanziData(hanziData);
    childs.add(child);
    // add the child
    hanziData.setUserAndHanzi(childs);
    hanziDaoImpl.insert(hanziData);
}
Also used : UserData(com.reinard.learnhanzi.models.UserData) UserAndHanzi(com.reinard.learnhanzi.models.UserAndHanzi) HanziData(com.reinard.learnhanzi.models.HanziData) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with UserData

use of com.reinard.learnhanzi.models.UserData 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 3 with UserData

use of com.reinard.learnhanzi.models.UserData in project learn-hanzi by reinardhz.

the class HanziDaoImplTest method testInsert1.

// Case 1: Insert a new data that is not exist in the database.
// @Test
public void testInsert1() throws Exception {
    logger.debug("Start test");
    String input = "對";
    HanziData hanziData = new HanziData();
    hanziData.setHanzi(input);
    hanziData.setCreated_date(System.currentTimeMillis());
    Set<UserAndHanzi> childs = new HashSet<UserAndHanzi>();
    UserAndHanzi child = new UserAndHanzi();
    UserData userData = new UserData();
    userData.setUser_id(1L);
    child.setUserData(userData);
    child.setHanziData(hanziData);
    childs.add(child);
    hanziData.setUserAndHanzi(childs);
    hanziDaoImpl.insert(hanziData);
    // make sure that data is inserted:
    HanziData result = hanziDaoImpl.selectBy(input);
    Assert.assertNotNull(result);
    Assert.assertEquals(result.getHanzi(), input);
    logger.debug("Result: " + result.toString());
}
Also used : UserData(com.reinard.learnhanzi.models.UserData) UserAndHanzi(com.reinard.learnhanzi.models.UserAndHanzi) HanziData(com.reinard.learnhanzi.models.HanziData) HashSet(java.util.HashSet)

Aggregations

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