From 3b55d06b89bf64873e685c3d78fce5affbba3d17 Mon Sep 17 00:00:00 2001 From: zheng_wenlong Date: Tue, 16 Apr 2019 11:20:38 +0900 Subject: Add warehouse server source code. [Patch Set 2] Add ReadMe.md Change-Id: I6ade52d2490f5ca4ba107c1a27ed6d5b39048725 Signed-off-by: zheng_wenlong --- .../app/market/web/services/SpringBaseService.java | 248 ++++++++++ .../web/services/account/AccountService.java | 76 +++ .../services/account/impl/AccountServiceImpl.java | 220 +++++++++ .../app/market/web/services/app/AppService.java | 124 +++++ .../web/services/app/impl/AppServiceImpl.java | 511 +++++++++++++++++++++ .../market/web/services/login/LoginService.java | 25 + .../web/services/login/impl/LoginServiceImpl.java | 81 ++++ .../app/market/web/services/main/MainService.java | 27 ++ .../web/services/main/impl/MainServiceImpl.java | 71 +++ .../app/market/web/services/user/UserService.java | 35 ++ .../web/services/user/impl/UserServiceImpl.java | 141 ++++++ 11 files changed, 1559 insertions(+) create mode 100644 warehouse/src/main/java/app/market/web/services/SpringBaseService.java create mode 100644 warehouse/src/main/java/app/market/web/services/account/AccountService.java create mode 100644 warehouse/src/main/java/app/market/web/services/account/impl/AccountServiceImpl.java create mode 100644 warehouse/src/main/java/app/market/web/services/app/AppService.java create mode 100644 warehouse/src/main/java/app/market/web/services/app/impl/AppServiceImpl.java create mode 100644 warehouse/src/main/java/app/market/web/services/login/LoginService.java create mode 100644 warehouse/src/main/java/app/market/web/services/login/impl/LoginServiceImpl.java create mode 100644 warehouse/src/main/java/app/market/web/services/main/MainService.java create mode 100644 warehouse/src/main/java/app/market/web/services/main/impl/MainServiceImpl.java create mode 100644 warehouse/src/main/java/app/market/web/services/user/UserService.java create mode 100644 warehouse/src/main/java/app/market/web/services/user/impl/UserServiceImpl.java (limited to 'warehouse/src/main/java/app/market/web/services') diff --git a/warehouse/src/main/java/app/market/web/services/SpringBaseService.java b/warehouse/src/main/java/app/market/web/services/SpringBaseService.java new file mode 100644 index 0000000..8c54aaf --- /dev/null +++ b/warehouse/src/main/java/app/market/web/services/SpringBaseService.java @@ -0,0 +1,248 @@ +/* + * Copyright (c) 2019 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.market.web.services; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import javax.servlet.http.HttpSession; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.web.client.HttpClientErrorException; + +import com.alibaba.fastjson.JSONObject; + +import app.market.WebServiceClient; +import app.market.model.app.App; +import app.market.utils.constants.Constants; +import app.market.utils.datatable.DataTableMap; +import app.market.utils.json.JsonMapperUtils; +import app.market.utils.json.JsonResult; +import app.market.utils.property.KeysConstants; +import app.market.utils.property.MessageUtil; +import app.market.utils.rest.RestTemplateUtil; +import app.market.utils.webservice.ApiParam; + +public abstract class SpringBaseService { + + protected String convertEmptyStr(Object obj) { + if ( obj == null ) { + return Constants.EMPTY_STRING_FALG; + } + if ( obj instanceof String ) { + String s = (String) obj; + return StringUtils.isEmpty( s ) ? Constants.EMPTY_STRING_FALG : s; + + } else if ( obj instanceof Integer ) { + Integer i = (Integer) obj; + return i == null ? Constants.EMPTY_STRING_FALG : i + ""; + + } else { + return obj.toString(); + } + } + + protected JsonResult validateAuthentication(HttpSession session) { + JsonResult jr = new JsonResult(); + RestTemplateUtil restTemplate = new RestTemplateUtil(); + try { + String jsonStr = restTemplate.get( WebServiceClient.REST_TOKEN_VALIDATETOKENAUTHORTICATION, session, new String[] {} ); + jr = JsonMapperUtils.readValue( jsonStr, JsonResult.class ); + return jr; + } catch ( Exception e ) { + if ( e instanceof HttpClientErrorException ) { + HttpClientErrorException hcee = (HttpClientErrorException) e; + switch ( hcee.getStatusCode().value() ) { + case Constants.STATUS_UNAUTHORIZED: + if (updateAccessToken(jr, session)) { + jr = validateAuthentication(session); + } else { + jr.setStatus(Constants.STATUS_UNAUTHORIZED); + jr.setData(MessageUtil.getPropertites(KeysConstants.STATUS_UNAUTHORIZED)); + } + break; + case Constants.STATUS_TOO_MANY_CONNECTIONS: + jr.setStatus( Constants.STATUS_TOO_MANY_CONNECTIONS ); + jr.setData( MessageUtil.getPropertites( KeysConstants.STATUS_TOO_MANY_CONNECTIONS ) ); + break; + case Constants.STATUS_FORBIDDEN: + jr.setStatus( Constants.STATUS_FORBIDDEN ); + jr.setData( MessageUtil.getPropertites( KeysConstants.STATUS_FORBIDDEN ) ); + break; + default: + jr.setStatus( Constants.STATUS_ERROR ); + jr.setData( MessageUtil.getPropertites( KeysConstants.PROJECT_ERROR ) ); + break; + } + } else { + jr.setStatus( Constants.STATUS_ERROR ); + jr.setData( MessageUtil.getPropertites( KeysConstants.PROJECT_ERROR ) ); + } + return jr; + } + } + + /** + * update acess token + * @param jr + * @param session + * @return + */ + private boolean updateAccessToken(JsonResult jr, HttpSession session) { + boolean ret = false; + RestTemplateUtil restTemplate = new RestTemplateUtil(); + try { + String refreshToken = (String) session.getAttribute(Constants.SESSION_REFRESH_TOKEN); + + String jsonStr = restTemplate.get(WebServiceClient.REST_TOKEN, session, new String[] { refreshToken }); + JSONObject json = JsonMapperUtils.getJsonObject(jsonStr); + String token = json.getString(ApiParam.API_RESPONSE_TOKEN); + //String refreshtoken = json.getString(ApiParam.API_RESPONSE_REFRESHTOKEN); + session.setAttribute( Constants.SESSION_TOKEN, token ); + //session.setAttribute( Constants.SESSION_REFRESH_TOKEN, refreshtoken ); + ret = true; + } catch (HttpClientErrorException e) { + switch (e.getStatusCode().value()) { + case Constants.STATUS_UNAUTHORIZED: + jr.setStatus(Constants.STATUS_UNAUTHORIZED); + jr.setData(MessageUtil.getPropertites(KeysConstants.STATUS_UNAUTHORIZED)); + break; + } + } catch (Exception e) { + e.printStackTrace(); + } + return ret; + } + +// private JsonResult updateAccessToken(JsonResult jr) { +// RestTemplateUtil restTemplate = new RestTemplateUtil(); +// try { +// String refreshToken = SpringWebUtil.getRequest().getParameter(ApiParam.API_RESPONSE_REFRESHTOKEN); +// String jsonStr = restTemplate.get(WebServiceClient.REST_TOKEN, new String[] { refreshToken }); +// jr.setStatus( Constants.STATUS_REFRESH_TOKEN ); +// jr.setData( jsonStr); +// } catch (HttpClientErrorException e) { +// switch (e.getStatusCode().value()) { +// case Constants.STATUS_UNAUTHORIZED: +// jr.setStatus( Constants.STATUS_UNAUTHORIZED ); +// jr.setData( MessageUtil.getPropertites( KeysConstants.STATUS_UNAUTHORIZED ) ); +// break; +// } +// } catch (Exception e) { +// e.printStackTrace(); +// } +// return jr; +// } + /** + * search start + * + * @param restURI + * @param obj + * @param model + * @param params + * @return + */ + protected String selectPaginationData(String restURI, Object obj, Object model, Map params, HttpSession session) { + Class form = obj.getClass(); + List dataList = new ArrayList(); + DataTableMap map = null; + List errList = new ArrayList(); + + try { + map = new DataTableMap(form.getMethod("getDraw", null).invoke(obj).toString(), dataList); + + RestTemplateUtil restTemplate = new RestTemplateUtil(); + String jsonStr = restTemplate.getByQueryParam(restURI, params, session); + + JSONObject json = JsonMapperUtils.getJsonObject(jsonStr); + int counter = json.getInteger(Constants.PAGINATION_COUNTER); + List list = json.getObject(Constants.PAGINATION_DATA, List.class); + + map.setTotal(counter); + dataList.addAll(list); + } catch ( Exception e ) { + exceptionHandler( e, map ); + } + return JsonMapperUtils.writeValueAsString( map.getMapData() ); + } + + protected void exceptionHandler(Exception e, DataTableMap map) { + if (e instanceof HttpClientErrorException) { + HttpClientErrorException hcee = (HttpClientErrorException) e; + if (hcee.getStatusCode().value() == Constants.STATUS_UNAUTHORIZED) { + map.setErrorMsg(Constants.STATUS_UNAUTHORIZED, + MessageUtil.getPropertites(KeysConstants.STATUS_UNAUTHORIZED)); + } else if (hcee.getStatusCode().value() == Constants.STATUS_BAD_REQUEST) { + map.setErrorMsg(Constants.STATUS_BAD_REQUEST, MessageUtil.getPropertites(KeysConstants.STATUS_BAD_REQUEST)); + } else { + map.setErrorMsg(Constants.STATUS_UNAUTHORIZED, MessageUtil.getPropertites(KeysConstants.PROJECT_ERROR)); + } + } else { + // failed + map.setErrorMsg(Constants.STATUS_ERROR, MessageUtil.getPropertites(KeysConstants.PROJECT_ERROR)); + } + } + + protected String exceptionHandler(Exception e, List msgList) { + if ( e instanceof HttpClientErrorException ) { + HttpClientErrorException hcee = (HttpClientErrorException) e; + // unauthorized + if ( hcee.getStatusCode().value() == Constants.STATUS_UNAUTHORIZED ) { + msgList.add( MessageUtil.getPropertites( KeysConstants.STATUS_UNAUTHORIZED ) ); + } else { + msgList.add( MessageUtil.getPropertites( KeysConstants.PROJECT_ERROR ) ); + } + return JsonMapperUtils.getJsonString( Constants.STATUS_ERROR, null, msgList ); + } else { + msgList.add( MessageUtil.getPropertites( KeysConstants.PROJECT_ERROR ) ); + return JsonMapperUtils.getJsonString( Constants.STATUS_ERROR, null, msgList ); + } + } + + protected void exceptionHandler(Exception e, JsonResult jr) { + jr.setStatus( Constants.STATUS_ERROR ); + if ( e instanceof HttpClientErrorException ) { + HttpClientErrorException hcee = (HttpClientErrorException) e; + // unauthorized + if ( hcee.getStatusCode().value() == Constants.STATUS_UNAUTHORIZED ) { + jr.setData( MessageUtil.getPropertites( KeysConstants.STATUS_UNAUTHORIZED ) ); + } else { + jr.setData( MessageUtil.getPropertites( KeysConstants.PROJECT_ERROR ) ); + } + } else { + jr.setData( MessageUtil.getPropertites( KeysConstants.PROJECT_ERROR ) ); + } + } + + protected void exceptionHandler(Exception e) throws Exception { + if ( e instanceof HttpClientErrorException ) { + HttpClientErrorException hcee = (HttpClientErrorException) e; + if ( hcee.getStatusCode().value() == Constants.STATUS_UNAUTHORIZED ) { + throw new Exception( MessageUtil.getPropertites( KeysConstants.STATUS_UNAUTHORIZED ) ); + } else { + throw new Exception( MessageUtil.getPropertites( KeysConstants.PROJECT_ERROR ) ); + } + } else { + throw new Exception( MessageUtil.getPropertites( KeysConstants.PROJECT_ERROR ) ); + } + } + + public void initPageList(Map model, boolean hasSpace) throws Exception { + // TODO Auto-generated method stub + + } +} diff --git a/warehouse/src/main/java/app/market/web/services/account/AccountService.java b/warehouse/src/main/java/app/market/web/services/account/AccountService.java new file mode 100644 index 0000000..69f8cc0 --- /dev/null +++ b/warehouse/src/main/java/app/market/web/services/account/AccountService.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2019 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.market.web.services.account; + +import java.util.Map; + +import javax.servlet.http.HttpSession; + +import app.market.utils.json.JsonResult; +import app.market.web.form.account.AccountForm; + +public interface AccountService { + + /** + * Authentication + * + * @return + */ + JsonResult validateAuthentication1(HttpSession session); + + /** + * Search AccountForm By UserId + * + * @param id + * @return + * @throws Exception + */ + AccountForm searchFormById(String id, HttpSession session) throws Exception; + + /** + * + * Page Search + * + * @param form + * @return + */ + String selectPaginationData(AccountForm form, HttpSession session); + + /** + * Account Delete + * + * @param id + * @return + */ + String delete(String id, HttpSession session); + + /** + * Account Save + * + * @param form + * @return + */ + String save(AccountForm form, HttpSession session); + + /** + * Get Account List + * + * @param model + * @param hasSpace + * @throws Exception + */ + void getAuthorityList(Map model, boolean hasSpace, HttpSession session) throws Exception; +} diff --git a/warehouse/src/main/java/app/market/web/services/account/impl/AccountServiceImpl.java b/warehouse/src/main/java/app/market/web/services/account/impl/AccountServiceImpl.java new file mode 100644 index 0000000..5d8e7ec --- /dev/null +++ b/warehouse/src/main/java/app/market/web/services/account/impl/AccountServiceImpl.java @@ -0,0 +1,220 @@ +/* + * Copyright (c) 2019 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.market.web.services.account.impl; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.http.HttpSession; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; +import org.springframework.web.client.HttpClientErrorException; + +import com.alibaba.fastjson.JSONObject; + +import app.market.WebServiceClient; +import app.market.model.user.User; +import app.market.utils.Utils; +import app.market.utils.constants.Constants; +import app.market.utils.datetime.DateTimeUtils; +import app.market.utils.json.JsonMapperUtils; +import app.market.utils.json.JsonResult; +import app.market.utils.property.KeysConstants; +import app.market.utils.property.MessageUtil; +import app.market.utils.property.Option; +import app.market.utils.property.PropertyUtil; +import app.market.utils.rest.RestTemplateUtil; +import app.market.utils.webservice.ApiParam; + +import app.market.web.controller.ControllerMapping; +import app.market.web.form.account.AccountForm; +import app.market.web.services.SpringBaseService; +import app.market.web.services.account.AccountService; + +@Service +public class AccountServiceImpl extends SpringBaseService implements AccountService { + private static Logger logger = LoggerFactory.getLogger(AccountServiceImpl.class); + + /** + * Authentication + * + * @return + */ + @Override + public JsonResult validateAuthentication1(HttpSession session) { + return super.validateAuthentication(session); + } + + /** + * + * Page Search + * + * @param form + * @return + */ + @Override + public String selectPaginationData(AccountForm form, HttpSession session) { + logger.debug("account list start --Class: " + this.getClass().getName() + "--method: " + + Thread.currentThread().getStackTrace()[1].getMethodName() + form.getUserId()); + String date = null; + if(form.getCreateDate() != null){ + date = DateTimeUtils.getDate(DateTimeUtils.DATE_FORMAT_YYYYMMDD,form.getCreateDate()); + } + Map param = new HashMap<>(); + param.put( ApiParam.API_USER_PARAM_USERNAME, form.getUserName() ); + param.put( ApiParam.API_USER_PARAM_AUID, form.getAuId() ); + param.put( ApiParam.API_USER_PARAM_MAIL, form.getMailAddress() ); + param.put( ApiParam.API_USER_PARAM_CREATEDATE, date ); + param.put( ApiParam.API_PARAM_ORDER, form.getOrder() ); + param.put( ApiParam.API_PARAM_SORT, form.getSort()); + param.put( ApiParam.API_PARAM_OFFSET, form.getOffset() ); + param.put( ApiParam.API_PARAM_LIMIT, form.getLimit() ); + param.put( ApiParam.API_APP_PARAM_KEYWORD, form.getKeyWord() ); + return super.selectPaginationData( WebServiceClient.REST_USER_SELECTPAGINATIONDATABYEXAMPLE, form, null, param, session); + } + + /** + * Account Save + * + * @param form + * @return + */ + @Override + public String save(AccountForm form, HttpSession session) { + logger.debug("save account start"); + User record = new User(); + String ret = JsonMapperUtils.getJsonString(Constants.STATUS_SUCCESS, null, null); + List msgList = new ArrayList(); + try { + Utils.reflect(form, record); + RestTemplateUtil restTemplate = new RestTemplateUtil(); + // Resttemplate Operation + String jsonStr = restTemplate.post(WebServiceClient.REST_USER, record, session); + + JSONObject json = JsonMapperUtils.getJsonObject(jsonStr); + String userId = json.getString(ApiParam.API_RESPONSE_APPID); + form.setUserId(userId); + + msgList.add(MessageUtil.getPropertites(KeysConstants.USER_SAVE_IS_SUCCESS)); + ret = JsonMapperUtils.getJsonString(Constants.STATUS_SUCCESS, ControllerMapping.ACCOUNT.toString(), msgList); + } catch (HttpClientErrorException e) { + if (e.getStatusCode().value() == Constants.STATUS_BAD_REQUEST) { + msgList.add(MessageUtil.getPropertites(KeysConstants.INVALID_QUERYPARAM)); + ret = JsonMapperUtils.getJsonString(Constants.STATUS_ERROR, null, msgList); + } + if (e.getStatusCode().value() == Constants.STATUS_ALREADY_EXISTS) { + msgList.add(MessageUtil.getPropertites(KeysConstants.RESOURCE_ALREADY_EXISTS)); + ret = JsonMapperUtils.getJsonString(Constants.STATUS_ALREADY_EXISTS, null, msgList); + } + return JsonMapperUtils.getJsonString(Constants.STATUS_ERROR, null, msgList); + } catch (Exception e) { + ret = exceptionHandler(e, msgList); + } + logger.debug("save account stop"); + return ret; + } + + /** + * Account Delete + * + * @param id + * @return + */ + @Override + public String delete(String id, HttpSession session) { + logger.debug("delete account start"); + List msgList = new ArrayList(); + try { + RestTemplateUtil restTemplate = new RestTemplateUtil(); + // Rest template operation + restTemplate.delete( WebServiceClient.REST_USER_BY_USERID, id, session ); + } catch (HttpClientErrorException e ) { + if(e.getStatusCode().value() == Constants.STATUS_ALREADY_EXISTS){ + msgList.add(MessageUtil.getPropertites(KeysConstants.USER_NOT_DELETE)); + return JsonMapperUtils.getJsonString(Constants.STATUS_ALREADY_EXISTS, null, msgList); + } + return JsonMapperUtils.getJsonString( Constants.STATUS_ERROR, null, "" ); + } catch (Exception e) { + e.printStackTrace(); + return JsonMapperUtils.getJsonString( Constants.STATUS_ERROR, null, "" ); + } + logger.debug("delete account stop "); + return JsonMapperUtils.getJsonString( Constants.STATUS_SUCCESS, null, "" ); + } + + /** + * Get Account List + * + * @param model + * @param hasSpace + * @throws Exception + */ + @Override + public void getAuthorityList(Map model,boolean hasSpace, HttpSession session) throws Exception { + logger.debug( "getAccounList" ); + try{ + RestTemplateUtil restTemplate = new RestTemplateUtil(); + List