use of com.reinard.learnhanzi.models.HanziData 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();
}
}
use of com.reinard.learnhanzi.models.HanziData in project learn-hanzi by reinardhz.
the class TestController method parseJson.
/**
* Convert Java Object to Json.
*
* @return json
*/
// @RequestMapping(value = "/parseJson", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
// @ResponseBody
public String parseJson() throws Exception {
HanziData hanziData = new HanziData();
hanziData.setHanzi_id(1L);
// hanziData.setCreated_date(new Timestamp(System.currentTimeMillis()));
hanziData.setHanzi("\u6211");
// An ObjectOutputStream writes primitive data types and graphs of Java
// objects to an OutputStream
OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectMapper objectMapper = new ObjectMapper();
// write the hanziData as Json in byteArrayOutputStream
objectMapper.writeValue(byteArrayOutputStream, hanziData);
byte[] data = ((ByteArrayOutputStream) byteArrayOutputStream).toByteArray();
String json = new String(data, "UTF-8");
System.out.println(json);
return json;
}
use of com.reinard.learnhanzi.models.HanziData 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());
}
use of com.reinard.learnhanzi.models.HanziData 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;
}
}
use of com.reinard.learnhanzi.models.HanziData in project learn-hanzi by reinardhz.
the class HanziDaoImpl method selectAll.
/**
* A method to select all data from "hanzi_data" table. This dao is tested.
*
* @return List<HanziData> All record from "hanzi_data" table. Return <i>null</i> if no data found.
* @throws Exception - If error happen when trying to select all data from database.
*/
@SuppressWarnings("all")
public List<HanziData> selectAll() throws Exception {
logger.info("Selecting all \"hanzi_data\"...");
Session newSession = hibernateSessionFactory.openSession();
Transaction transaction = null;
try {
transaction = newSession.beginTransaction();
List<HanziData> result = newSession.createCriteria(HanziData.class).addOrder(Order.desc("created_date")).list();
transaction.commit();
if (result == null || result.isEmpty()) {
logger.info("Select all \"hanzi_data\" produce no data.");
logger.info("Select all \"hanzi_data\" succeed.");
return null;
} else {
logger.info("Select all \"hanzi_data\" succeed.");
logger.debug(result.toString());
return result;
}
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
logger.error("Unexpected error occurred when trying to select all from \"hanzi_data\" table, rollback succeed", e);
} else {
logger.error("Unexpected error occurred when trying to select all from \"hanzi_data\" table.", e);
}
throw e;
} finally {
if (newSession.isOpen())
newSession.close();
}
}
Aggregations