Java tutorial
/* * Copyright (c) 2013, Helome and/or its affiliates. All rights reserved. * Helome PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * Created on 2014-5-26 */ package mobile.service; import java.io.File; import java.util.Iterator; import java.util.List; import mobile.service.result.ServiceResult; import mobile.service.result.ServiceVOResult; import mobile.util.ServiceResults; import mobile.vo.result.CommonVO; import mobile.vo.user.SelfInfo; import models.Expert; import models.User; import org.apache.commons.lang3.StringUtils; import play.Logger; import play.Logger.ALogger; import play.libs.Json; import play.mvc.Http.Context; import play.mvc.Http.Session; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import controllers.attachment.AttachmentApp; import controllers.base.ObjectNodeResult; import controllers.user.UserAvatarApp; import exception.AvatarException; import ext.usercenter.UserAuthService; /** * * * @ClassName: SelfInfoService * @Description: ? * @date 2014-5-26 ?6:06:55 * @author ShenTeng * */ public class SelfInfoService { private static final ALogger LOGGER = Logger.of(SelfInfoService.class); /** * ??? * * @return SelfInfo VO */ public static SelfInfo getSelfInfo() { Session session = Context.current().session(); User user = User.getFromSession(session); String token = UserAuthService.getTokenInSession(session); return SelfInfo.create(user, token); } /** * ???? * * @param jobExpArray * @return */ public static ServiceResult saveJobExp(ArrayNode jobExpArray) { ArrayNode newJobExpArray = jobExpArray.deepCopy(); Iterator<JsonNode> elements = newJobExpArray.elements(); while (elements.hasNext()) { ObjectNode next = (ObjectNode) elements.next(); if (next.hasNonNull("endYear") && next.get("endYear").asText().equals("-1")) { next.put("endYear", ""); } } ObjectNode jobExpNode = Json.newObject(); jobExpNode.set("jobExp", newJobExpArray); ObjectNodeResult objectNodeResult = Expert.saveExpertByJson(Context.current().session(), jobExpNode); return ServiceResult.create(objectNodeResult); } /** * ??? * * @param newJobExp ?? */ public static ServiceResult addJobExp(JsonNode newJobExp) { User user = User.getFromSession(Context.current().session()); Expert expert = Expert.findByUserId(user.id); ArrayNode jobExpArrayNode = null; if (null == expert || StringUtils.isBlank(expert.jobExp)) { jobExpArrayNode = Json.newObject().arrayNode(); } else { jobExpArrayNode = (ArrayNode) Json.parse(expert.jobExp); } if (newJobExp.hasNonNull("endYear") && newJobExp.get("endYear").asText().equals("-1")) { ((ObjectNode) newJobExp).put("endYear", ""); } jobExpArrayNode.add(newJobExp); ObjectNode jobExpNode = Json.newObject(); jobExpNode.set("jobExp", jobExpArrayNode); ObjectNodeResult objectNodeResult = Expert.saveExpertByJson(Context.current().session(), jobExpNode); return ServiceResult.create(objectNodeResult); } /** * ?????? * * @param index ????,0 */ public static ServiceResult deleteJobExpByIndex(int index) { User user = User.getFromSession(Context.current().session()); Expert expert = Expert.findByUserId(user.id); ArrayNode jobExpArrayNode = null; if (null == expert || StringUtils.isBlank(expert.jobExp) || index < 0) { return ServiceResult.error("234001", "????"); } else { jobExpArrayNode = (ArrayNode) Json.parse(expert.jobExp); if (index >= jobExpArrayNode.size()) { return ServiceResult.error("234001", "????"); } else { jobExpArrayNode.remove(index); } } ObjectNode jobExpNode = Json.newObject(); jobExpNode.set("jobExp", jobExpArrayNode); ObjectNodeResult objectNodeResult = Expert.saveExpertByJson(Context.current().session(), jobExpNode); return ServiceResult.create(objectNodeResult); } /** * ??? * * @param eduExpArray * @return */ public static ServiceResult saveEducationExp(ArrayNode eduExpArray) { ObjectNode eduExpNode = Json.newObject(); eduExpNode.set("educationExp", eduExpArray); ObjectNodeResult objectNodeResult = Expert.saveExpertByJson(Context.current().session(), eduExpNode); return ServiceResult.create(objectNodeResult); } /** * ?? * * @param newEduExp ?Json */ public static ServiceResult addEducationExp(JsonNode newEduExp) { User user = User.getFromSession(Context.current().session()); Expert expert = Expert.findByUserId(user.id); ArrayNode eduExpArrayNode = null; if (null == expert || StringUtils.isBlank(expert.educationExp)) { eduExpArrayNode = Json.newObject().arrayNode(); } else { eduExpArrayNode = (ArrayNode) Json.parse(expert.educationExp); } eduExpArrayNode.add(newEduExp); ObjectNode eduExpNode = Json.newObject(); eduExpNode.set("educationExp", eduExpArrayNode); ObjectNodeResult objectNodeResult = Expert.saveExpertByJson(Context.current().session(), eduExpNode); return ServiceResult.create(objectNodeResult); } /** * ????? * * @param index ???,0 */ public static ServiceResult deleteEduExpByIndex(int index) { User user = User.getFromSession(Context.current().session()); Expert expert = Expert.findByUserId(user.id); ArrayNode eduExpArrayNode = null; if (null == expert || StringUtils.isBlank(expert.educationExp) || index < 0) { return ServiceResult.error("232001", "???"); } else { eduExpArrayNode = (ArrayNode) Json.parse(expert.educationExp); if (index >= eduExpArrayNode.size()) { return ServiceResult.error("232001", "???"); } else { eduExpArrayNode.remove(index); } } ObjectNode eduExpNode = Json.newObject(); eduExpNode.set("educationExp", eduExpArrayNode); ObjectNodeResult objectNodeResult = Expert.saveExpertByJson(Context.current().session(), eduExpNode); return ServiceResult.create(objectNodeResult); } /** * ? * * @param email * @param pwd ? * @return */ public static ServiceResult completeUserInfo(String email, String pwd) { ObjectNodeResult result = User.completeUserInfo(Context.current().session(), email, pwd); return ServiceResult.create(result); } /** * ??,???Json * * @param node ?Json * @return */ public static ServiceResult saveUserInfo(JsonNode node) { if (node.hasNonNull("language")) { String language = node.get("language").asText(); List<String> list = AttachmentApp.getLanguage(); if (!list.contains(language)) { return ServiceResults.illegalParameters("?language" + language); } } ObjectNodeResult result = Expert.saveExpertByJson(Context.current().session(), node); return ServiceResult.create(result); } /** * ? * * @param avatarFile ? * @return */ public static ServiceVOResult<CommonVO> uploadAvatar(File avatarFile) { User current = User.getFromSession(Context.current().session()); ObjectNodeResult result = new ObjectNodeResult(); try { UserAvatarApp.save(avatarFile, result, current); } catch (AvatarException e) { LOGGER.error("??", e); return ServiceVOResult.error("215001", "?"); } ServiceVOResult<CommonVO> serviceVOResult = ServiceVOResult.create(result); if (serviceVOResult.isSuccess()) { ObjectNode objectNode = result.getObjectNode(); CommonVO vo = CommonVO.create(); vo.set("avatar_190", objectNode.path("avatar_190").asText()); vo.set("avatar_70", objectNode.path("avatar_70").asText()); vo.set("avatar_22", objectNode.path("avatar_22").asText()); serviceVOResult.setVo(vo); } return serviceVOResult; } }