From 9d932fb6dbeefcc9d8eb96322f410241d4450d15 Mon Sep 17 00:00:00 2001 From: Salman Manoe Date: Thu, 19 Dec 2024 15:39:42 +0700 Subject: [PATCH] Update GenericServiceController.java Clean code --- .../controller/GenericServiceController.java | 238 +++++++----------- 1 file changed, 85 insertions(+), 153 deletions(-) diff --git a/jasamedika-web/src/main/java/com/jasamedika/medifirst2000/controller/GenericServiceController.java b/jasamedika-web/src/main/java/com/jasamedika/medifirst2000/controller/GenericServiceController.java index fc730def..eabeacd4 100644 --- a/jasamedika-web/src/main/java/com/jasamedika/medifirst2000/controller/GenericServiceController.java +++ b/jasamedika-web/src/main/java/com/jasamedika/medifirst2000/controller/GenericServiceController.java @@ -1,107 +1,52 @@ package com.jasamedika.medifirst2000.controller; -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import com.jasamedika.medifirst2000.base.vo.BaseModelVO; +import com.jasamedika.medifirst2000.controller.base.LocaleController; +import com.jasamedika.medifirst2000.exception.ServiceVOException; +import com.jasamedika.medifirst2000.service.ModelService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; import javax.persistence.Column; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; import javax.servlet.http.HttpServletRequest; +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; -import org.joda.time.chrono.AssembledChronology.Fields; -import org.json.JSONException; -import org.json.JSONObject; -import org.scalastuff.scalabeans.sig.Mirror.Annotation; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cache.annotation.Cacheable; -import org.springframework.context.MessageSource; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; - -import com.jasamedika.medifirst2000.base.vo.BaseMasterVO; -import com.jasamedika.medifirst2000.base.vo.BaseModelVO; -import com.jasamedika.medifirst2000.base.vo.BaseTransactionVO; -import com.jasamedika.medifirst2000.controller.base.LocaleController; -import com.jasamedika.medifirst2000.service.ModelService; -import com.jasamedika.medifirst2000.util.JsonUtil; -import com.jasamedika.medifirst2000.util.rest.RestUtil; -import com.jasamedika.medifirst2000.vo.AntrianPasienRegistrasiVO; -import com.jasamedika.medifirst2000.vo.PasienDaftarVO; - -import java.lang.reflect.Method; -import java.net.URLDecoder; +import static com.jasamedika.medifirst2000.util.JsonUtil.ToMaps; +import static com.jasamedika.medifirst2000.util.rest.RestUtil.getJsonResponse; +import static java.net.URLDecoder.decode; +import static org.springframework.http.HttpStatus.CREATED; +import static org.springframework.http.HttpStatus.OK; +import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; +import static org.springframework.web.bind.annotation.RequestMethod.GET; @RestController @RequestMapping("/service") -public class GenericServiceController extends LocaleController { +public class GenericServiceController extends LocaleController { - @Autowired - private MessageSource messageSource; + private static final String CONTENT_TYPE = "Content-Type"; @Autowired private ModelService modelService; - private static final String CONTENT_TYPE = "Content-Type"; - public static List GetFields(Class data) { - List items = new ArrayList(); - String name = data.getName(); - Class parent = data.getSuperclass(); - if (parent instanceof Class) { - name = ((Class) parent).getName(); - } - - Class tmpClass = null; - if (BaseModelVO.class.isAssignableFrom(data.getClass())) { - tmpClass = BaseTransactionVO.class; - } else if (BaseTransactionVO.class.isAssignableFrom(data.getClass())) { - tmpClass = BaseMasterVO.class; - } - - if ((data == tmpClass)) { - for (Field field : data.getDeclaredFields()) { - items.add(field); - } - return items; - } - - if (parent != null) { - for (Field field : GetFields(data.getSuperclass())) { - items.add(field); - } - } - - for (Field field : data.getDeclaredFields()) { - items.add(field); - } - return items; - } - @SuppressWarnings("unchecked") - @RequestMapping(value = "/get-setting/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) + @RequestMapping(value = "/get-setting/", method = GET, produces = APPLICATION_JSON_VALUE) public ResponseEntity getData(@RequestParam(value = "prefix", required = false) String prefix) { - - - return RestUtil.getJsonResponse(GetSettingDataFixed(prefix), HttpStatus.CREATED, mapHeaderMessage); + return getJsonResponse(GetSettingDataFixed(prefix), CREATED, mapHeaderMessage); } - @RequestMapping(value = "/list-generic/", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) // &skip={skip}&page={page}&pageSize={pageSize}&filter[logic]={logic}&filter[filters][0][value]={value}&filter[filters][0][field]={fields}&filter[filters][0][operator]={operator}&filter[filters][0][ignoreCase]={ignorecase}", - // method - // = - // RequestMethod.GET, - // produces - // = - // MediaType.APPLICATION_JSON_VALUE) -// @Cacheable ("default") - public ResponseEntity>> listGeneric(@RequestParam(value = "view", required = false) String entity, + @RequestMapping(value = "/list-generic/", method = GET, produces = APPLICATION_JSON_VALUE) + public ResponseEntity>> listGeneric( + @RequestParam(value = "view", required = false) String entity, @RequestParam(value = "select", required = false) String field, @RequestParam(value = "take", required = false) Integer take, @RequestParam(value = "skip", required = false) Integer skip, @@ -113,82 +58,69 @@ public class GenericServiceController extends LocaleController=0) - { - if(criteria==null) - criteria=query.replace("filter[filters][0][filter][field]=", ""); - else - criteria+=","+query.replace("filter[filters][0][filter][field]=", ""); - - - } - else if(query.indexOf("filter[filters][0][filter][value]")>=0) - { - if(values==null) - values=query.replace("filter[filters][0][filter][value]=", ""); - else - values+=","+query.replace("filter[filters][0][filter][value]=", ""); + StringBuilder criteriaBuilder = new StringBuilder(criteria); + StringBuilder valuesBuilder = new StringBuilder(values); + for (String query : arrQueries) { + if (query.contains("filter[filters][0][filter][field]")) { + criteriaBuilder.append(",").append(query.replace("filter[filters][0][filter][field]=", "")); + } else if (query.contains("filter[filters][0][filter][value]")) { + valuesBuilder.append(",").append(query.replace("filter[filters][0][filter][value]=", "")); } } - // map.put("data", src); + values = valuesBuilder.toString(); + criteria = criteriaBuilder.toString(); if (field.equals("*")) { - field = ""; - - for (Field fieldItem : GetFields( Class.forName("com.jasamedika.medifirst2000.entities." + entity))) { - { - String name = fieldItem.getName(); - if(name.equals("serialVersionsUID"))continue; - Boolean valid = false; - for (java.lang.annotation.Annotation annotation : fieldItem.getDeclaredAnnotations()) { - if (annotation instanceof JoinColumn) { - valid=true; - } else if (annotation instanceof Column) { - Column column = (Column)annotation; - if(column.name().endsWith("Fk")) - if(fieldItem.getName().endsWith("Id")==false) - valid=true; - }else if (annotation instanceof OneToMany) { - - valid=true; - } - + StringBuilder fieldBuilder = new StringBuilder(); + for (Field fieldItem : GetFields(Class.forName("com.jasamedika.medifirst2000.entities." + entity))) { + String name = fieldItem.getName(); + if (name.equals("serialVersionsUID")) + continue; + boolean valid = false; + for (Annotation annotation : fieldItem.getDeclaredAnnotations()) { + if (annotation instanceof JoinColumn) { + valid = true; + } else if (annotation instanceof Column) { + Column column = (Column) annotation; + if (column.name().endsWith("Fk")) + if (!fieldItem.getName().endsWith("Id")) + valid = true; + } else if (annotation instanceof OneToMany) { + valid = true; } - if(valid ==false) - if (field.equals("")) - field += fieldItem.getName(); - else - field += "," + fieldItem.getName(); } -// field+=",id"; + if (!valid) + if (fieldBuilder.toString().isEmpty()) + fieldBuilder.append(fieldItem.getName()); + else + fieldBuilder.append(",").append(fieldItem.getName()); } - }else if (field.equals("**")) { - field="*"; + field = fieldBuilder.toString(); + } else if (field.equals("**")) { + field = "*"; + } + List> modelGenericVO = modelService.getAllData(entity, field, take, skip, page, pageSize, + logic, value, fieldS, operator, ignorecase, criteria, values); + HttpHeaders headers = new HttpHeaders(); + headers.set(CONTENT_TYPE, APPLICATION_JSON_VALUE); + try { + return new ResponseEntity<>(ToMaps(modelGenericVO), headers, OK); + } catch (IllegalArgumentException | IllegalAccessException e) { + throw new ServiceVOException(e.getMessage()); } - - - // for dateformat using [date] - List> modelGenericVO = modelService.getAllData(entity, field, take, skip, page, pageSize, logic, value, - fieldS, operator, ignorecase, criteria, values); - HttpHeaders headers = new HttpHeaders(); - - // headers.add(ACCESS_CONTROL_ALLOW_ORIGIN, "*"); - headers.set(CONTENT_TYPE,MediaType.APPLICATION_JSON_VALUE); - Map map = new HashMap();try { - return new ResponseEntity(JsonUtil.ToMaps(modelGenericVO),headers,HttpStatus.OK); - } catch (IllegalArgumentException | IllegalAccessException e) { - e.printStackTrace(); } - // return RestUtil.getJsonResponse(modelGenericVO, HttpStatus.OK); - return null; -} + + public static List GetFields(Class data) { + List items = new ArrayList<>(); + Class parent = data.getSuperclass(); + if (parent != null) + items.addAll(GetFields(data.getSuperclass())); + items.addAll(Arrays.asList(data.getDeclaredFields())); + return items; + } }