use of com.reinard.learnhanzi.models.HanziData in project learn-hanzi by reinardhz.
the class HanziDaoImplTest method testSelectBy.
@SuppressWarnings("all")
@Test
public void testSelectBy() throws Exception {
logger.debug("Start test");
// Case 1: Data Found:
HanziData resultFound = hanziDaoImpl.selectBy("我");
Assert.assertNotNull(resultFound);
Assert.assertEquals(resultFound.getHanzi(), "我");
logger.debug("Result: " + resultFound.toString());
// Case 2: Data Not Found:
HanziData resultNotFound = hanziDaoImpl.selectBy("鳱");
Assert.assertNull(resultNotFound);
}
use of com.reinard.learnhanzi.models.HanziData 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);
}
use of com.reinard.learnhanzi.models.HanziData 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;
}
}
use of com.reinard.learnhanzi.models.HanziData 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;
}
}
use of com.reinard.learnhanzi.models.HanziData in project learn-hanzi by reinardhz.
the class TestController method parseBigJson.
/**
* Use this code, it is the fastest to parse big json data.
*
* @return
* @throws Exception
*/
// @RequestMapping(value = "/parseBigJson", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8" })
// @ResponseBody
public String parseBigJson() throws Exception {
StringBuilder stringbuilder = new StringBuilder();
OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectMapper objectMapper = new ObjectMapper();
for (int i = 0; i <= 3000; i++) {
HanziData hanziData = new HanziData();
hanziData.setHanzi_id(1L);
// hanziData.setCreated_date(new Timestamp(System.currentTimeMillis()));
hanziData.setHanzi("\u6211");
byteArrayOutputStream = new ByteArrayOutputStream();
objectMapper.writeValue(byteArrayOutputStream, hanziData);
byte[] data = ((ByteArrayOutputStream) byteArrayOutputStream).toByteArray();
String json = new String(data, "UTF-8");
stringbuilder.append(json + "\n");
// faster that calling byteArrayOutputStream.close();
byteArrayOutputStream = null;
return "";
}
return stringbuilder.toString();
}
Aggregations