123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- package com.lemon.lifecenter.common;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.io.UnsupportedEncodingException;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLDecoder;
- import java.net.URLEncoder;
- import java.security.spec.AlgorithmParameterSpec;
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Base64;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Random;
- import java.util.UUID;
- import javax.crypto.Cipher;
- import javax.crypto.Mac;
- import javax.crypto.spec.IvParameterSpec;
- import javax.crypto.spec.SecretKeySpec;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import com.lemon.lifecenter.dto.PatientDTO;
- public class LifeCenterFunction {
- public static String setURLEncode(String content, String lngType) throws UnsupportedEncodingException {
- return URLEncoder.encode(content, lngType);
- }
- public static String setURLDecode(String content, String lngType) throws UnsupportedEncodingException {
- return URLDecoder.decode(content, lngType);
- }
- public static String aesEncrypt(String encKey, byte[] iv, String paramInput) throws Exception {
- Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
- SecretKeySpec key = new SecretKeySpec(encKey.getBytes("UTF-8"), "AES");
- cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
- byte[] cipherText = cipher.doFinal(paramInput.getBytes("UTF-8"));
- return Base64.getEncoder().encodeToString(cipherText);
- }
- public static String aesDecrypt(String encKey, byte[] iv, String paramInput) throws Exception {
- byte[] textBytes = Base64.getDecoder().decode(paramInput);
- AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
- SecretKeySpec newKey = new SecretKeySpec(encKey.getBytes("UTF-8"), "AES");
- Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
- cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
- return new String(cipher.doFinal(textBytes), "UTF-8");
- }
- public static ArrayList<HashMap<String, String>> parseJSON(String jsonString) {
- ArrayList<HashMap<String, String>> rtn = new ArrayList<HashMap<String, String>>();
- try {
- JSONArray array = new JSONArray(jsonString);
- for (int i = 0; i < array.length(); i++) {
- HashMap<String, String> map = new HashMap<String, String>();
- JSONObject object = array.getJSONObject(i);
- Iterator<?> keys = object.keys();
- while (keys.hasNext()) {
- String key = (String) keys.next();
- String val = "";
- if (object.isNull(key)) {
- val = "";
- } else {
- if (object.get(key) instanceof Integer) {
- val = String.valueOf(object.getInt(key));
- } else if (object.get(key) instanceof Long) {
- val = String.valueOf(object.getLong(key));
- } else if (object.get(key) instanceof Double) {
- val = String.valueOf(object.getDouble(key));
- } else {
- val = object.getString(key).equals(null) ? "" : object.getString(key);
- }
- }
- map.put(key, val);
- }
- rtn.add(i, map);
- map = null;
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return rtn;
- }
- public static String getRandomUUID() {
- return UUID.randomUUID().toString().replaceAll("-", "");
- }
- public static String exportOnlyNumber(String input) {
- return input.replaceAll("[^0-9?!\\.]", "");
- }
- public static String getNow() {
- DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date date = new Date();
- return dateFormat.format(date);
- }
-
- public static String getPrevDate( String ymd ) throws ParseException {
- DateFormat df = new SimpleDateFormat( "yyyy-MM-dd" );
- Date date = df.parse( ymd );
-
- Calendar cal = Calendar.getInstance();
- cal.setTime( date );
- cal.add( Calendar.DATE, -1 );
-
- return df.format( cal.getTime() );
- }
-
- public static String getNextDate( String ymd ) throws ParseException {
- DateFormat df = new SimpleDateFormat( "yyyy-MM-dd" );
- Date date = df.parse( ymd );
-
- Calendar cal = Calendar.getInstance();
- cal.setTime( date );
- cal.add( Calendar.DATE, +1 );
-
- return df.format( cal.getTime() );
- }
- public static String getNow(String format) {
- DateFormat dateFormat = new SimpleDateFormat(format);
- Date date = new Date();
- return dateFormat.format(date);
- }
- public static long getNowUnixTimeStamp() {
- return System.currentTimeMillis() / 1000;
- }
- public static long setFormatUnixTimeStamp(String data) {
- DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- long timeStamp = 0;
- try {
- long time = dateFormat.parse(data).getTime();
- timeStamp = time / 1000;
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return timeStamp;
- }
- public static String setFormatDateFromUnixTimeStamp(Date data) {
- SimpleDateFormat transFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- return transFormat.format(data);
- }
- public static String getRandomNumber(int maxSize) {
- Random rand = new Random();
- String code = "";
- for (int i = 0; i < maxSize; i++) {
- code += rand.nextInt(9);
- }
- return code;
- }
- public static void scriptMessage(HttpServletResponse response, String script) {
- response.setContentType("text/html; charset=UTF-8");
- PrintWriter out;
- try {
- out = response.getWriter();
- out.println("<script>" + script + "</script>");
- out.flush();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static String getRemoteAddr(HttpServletRequest request) {
- String ip = request.getHeader("X-Forwarded-For");
- if (ip == null) {
- ip = request.getHeader("Proxy-Client-IP");
- }
- if (ip == null) {
- ip = request.getHeader("WL-Proxy-Client-IP");
- }
- if (ip == null) {
- ip = request.getHeader("HTTP_CLIENT_IP");
- }
- if (ip == null) {
- ip = request.getHeader("HTTP_X_FORWARDED_FOR");
- }
- if (ip == null) {
- ip = request.getRemoteAddr();
- }
- return ip;
- }
- public static String setPhoneFormat(String number) {
- StringBuilder sb = new StringBuilder(15);
- StringBuilder temp = new StringBuilder(number);
- int tempLength = temp.length();
- if (tempLength == 3) {
- sb.append(number);
- } else {
- while (tempLength < 10) {
- temp.insert(0, "0");
- }
- char[] chars = temp.toString().toCharArray();
- int size = chars.length;
- if (size < 11) {
- for (int i = 0; i < size; i++) {
- if (i == 3) {
- sb.append("-");
- } else if (i == 6) {
- sb.append("-");
- }
- sb.append(chars[i]);
- }
- } else {
- for (int i = 0; i < size; i++) {
- if (i == 3) {
- sb.append("-");
- } else if (i == 7) {
- sb.append("-");
- }
- sb.append(chars[i]);
- }
- }
- }
- return sb.toString();
- }
-
- public static String removeTag(String html) throws Exception {
- return html.replaceAll("<(/)?([a-zA-Z]*)(\\s[a-zA-Z]*=[^>]*)?(\\s)*(/)?>", "");
- }
-
- public static String getSalt() {
- String uniqId = "";
- Random randomGenerator = new Random();
- // length - set the unique Id length
- for (int length = 1; length <= 10; ++length) {
- int randomInt = randomGenerator.nextInt(10); // digit range from 0 - 9
- uniqId += randomInt + "";
- }
- return uniqId;
- }
-
- public static String getTimestamp() {
- long timestamp_long = System.currentTimeMillis() / 1000;
- String timestamp = Long.toString(timestamp_long);
- return timestamp;
- }
-
- private static final Logger logger = LoggerFactory.getLogger(LifeCenterFunction.class);
- public static String getSignature(String apiSecret, String salt, String timestamp) throws Exception {
- String signature = "";
- try {
- String temp = timestamp + salt;
- logger.error("temp -- > " + temp);
- SecretKeySpec keySpec = new SecretKeySpec(apiSecret.getBytes(), "HmacSHA256");
- Mac mac = Mac.getInstance("HmacSHA256");
- mac.init(keySpec);
- // 바이너리를 hex로 변환
- byte[] result = mac.doFinal(temp.getBytes());
- char[] hexArray = "0123456789ABCDEF".toCharArray();
- char[] hexChars = new char[result.length * 2];
- for (int i = 0; i < result.length; i++) {
- int positive = result[i] & 0xff;
- hexChars[i * 2] = hexArray[positive >>> 4];
- hexChars[i * 2 + 1] = hexArray[positive & 0x0F];
- }
- signature = new String(hexChars);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return signature.toLowerCase();
- }
-
- public static String httpUrlConnection(String apiUrl, HashMap<String, String> hash) {
- String result = "";
- URL url;
- try {
- url = new URL(apiUrl);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- conn.setRequestProperty("Accept", "application/json");
- conn.setRequestMethod("POST"); // post방식 통신
- conn.setDoOutput(true); // 쓰기모드 지정
- conn.setDoInput(true); // 읽기모드 지정
- conn.setUseCaches(false); // 캐싱데이터를 받을지 안받을지
- conn.setDefaultUseCaches(false); // 캐싱데이터 디폴트 값 설정
- conn.setReadTimeout(10000); // 타임아웃 10초
-
- OutputStream os = conn.getOutputStream(); // 서버로 보내기 위한 출력 스트림
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); // UTF-8로 전송
- bw.write(getPostString(hash)); // 매개변수 전송
- bw.flush();
- bw.close();
- os.close();
- InputStream is = conn.getInputStream(); //데이타를 받기위 구멍을 열어준다
- StringBuilder builder = new StringBuilder(); //문자열을 담기 위한 객체
- BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8")); //문자열 셋 세팅
- String line;
- while ((line = reader.readLine()) != null) {
- builder.append(line+ "\n");
- }
- // logger.error("httpUrlConnection Result -- > " + builder.toString());
- result = builder.toString();
- conn.disconnect();
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
-
- return result;
- }
-
- private static String getPostString(HashMap<String, String> map) {
- StringBuilder result = new StringBuilder();
- boolean first = true; // 첫 번째 매개변수 여부
- for (Map.Entry<String, String> entry : map.entrySet()) {
- if (first)
- first = false;
- else // 첫 번째 매개변수가 아닌 경우엔 앞에 &를 붙임
- result.append("&");
- try { // UTF-8로 주소에 키와 값을 붙임
- result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
- result.append("=");
- result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
- } catch (UnsupportedEncodingException ue) {
- ue.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return result.toString();
- }
- public static String phone(String src) {
- if (src == null) {
- return "";
- }
- if (src.length() == 8) {
- return src.replaceFirst("^([0-9]{4})([0-9]{4})$", "$1-$2");
- } else if (src.length() == 12) {
- return src.replaceFirst("(^[0-9]{4})([0-9]{4})([0-9]{4})$", "$1-$2-$3");
- }
- return src.replaceFirst("(^02|[0-9]{3})([0-9]{3,4})([0-9]{4})$", "$1-$2-$3");
- }
-
- public static String getDisease(PatientDTO dto) {
- String highBloodPressureCheck = dto.getHighBloodPressureCheck().equals("Y") ? "고혈압" : "";
- String lowBloodPressureCheck = dto.getLowBloodPressureCheck().equals("Y") ? "저혈압" : "";
- String organTransplantCheck = dto.getOrganTransplantCheck().equals("Y") ? "장기이식(신장, 간 등)" : "";
- String diabetesCheck = dto.getDiabetesCheck().equals("Y") ? "당뇨" : "";
- String respiratoryDiseaseCheck = dto.getRespiratoryDiseaseCheck().equals("Y") ? "호흡기질환" : "";
- String immunologicalDiseaseCheck = dto.getImmunologicalDiseaseCheck().equals("Y") ? "면역질환(류마티스 등)" : "";
- String heartDisease = dto.getHeartDisease().equals("Y") ? "심장질환" : "";
- String liverDisease = dto.getLiverDisease().equals("Y") ? "간질환" : "";
- String operation = dto.getOperation().equals("Y") ? "수술(" + dto.getOperationContent() + ")" : "";
- String allergyCheck = dto.getAllergyCheck().equals("Y") ? "알레르기" : "";
- String kidneyDisease = dto.getKidneyDisease().equals("Y") ? "신장질환" : "";
- String cancerName = dto.getCancerName() == null ? "" : "(" + dto.getCancerName() + ")";
- String cancerCheck = dto.getCancerCheck().equals("Y") ? "암" + cancerName : "";
- String etcContent = dto.getEtcContentDisease() == null ? "" : dto.getEtcContentDisease();
- String ectCheckDisease = dto.getEtcCheckDisease().equals("Y") ? "기타(" + etcContent + ")" : "";
-
- ArrayList<String> disease = new ArrayList<String>();
- disease.add(highBloodPressureCheck);
- disease.add(lowBloodPressureCheck);
- disease.add(organTransplantCheck);
- disease.add(diabetesCheck);
- disease.add(respiratoryDiseaseCheck);
- disease.add(immunologicalDiseaseCheck);
- disease.add(heartDisease);
- disease.add(liverDisease);
- disease.add(operation);
- disease.add(allergyCheck);
- disease.add(kidneyDisease);
- disease.add(cancerCheck);
- disease.add(ectCheckDisease);
-
- String strDisease = "";
- for (int i = 0; i < disease.size(); i++) {
- String str = disease.get(i);
- if (!str.equals("")) {
- strDisease += str;
- strDisease += ", ";
- }
- }
-
- strDisease = strDisease.trim();
- if (!strDisease.equals("")) {
- strDisease = strDisease.substring(0, strDisease.length()-1);
- }
-
- return strDisease;
- }
-
- public static String getSymptom(PatientDTO dto) {
- String feverCheck = dto.getFeverCheck().equals("Y") ? "열감(열나는 느낌)" : "";
- String coughCheck = dto.getCoughCheck().equals("Y") ? "기침" : "";
- String colic = dto.getColic().equals("Y") ? "복통(배아픔)" : "";
- String coldFitCheck = dto.getColdFitCheck().equals("Y") ? "오한(추운 느낌)" : "";
- String sputumCheck = dto.getSputumCheck().equals("Y") ? "가래" : "";
- String ocinCheck = dto.getOcinCheck().equals("Y") ? "오심(구역질)" : "";
- String chestPain = dto.getChestPain().equals("Y") ? "흉통" : "";
- String noseCheck = dto.getNoseCheck().equals("Y") ? "콧물 또는 코 막힘" : "";
- String vomitingCheck = dto.getVomitingCheck().equals("Y") ? "구토" : "";
- String musclePainCheck = dto.getMusclePainCheck().equals("Y") ? "근육통(몸살)" : "";
- String soreThroatCheck = dto.getSoreThroatCheck().equals("Y") ? "인후통(목 아픔)" : "";
- String diarrheaCheck = dto.getDiarrheaCheck().equals("Y") ? "설사" : "";
- String headacheCheck = dto.getHeadacheCheck().equals("Y") ? "두통(머리아픔)" : "";
- String dyspneaCheck = dto.getDyspneaCheck().equals("Y") ? "호흡곤란(숨가쁨)" : "";
- String fatigueCheck = dto.getFatigueCheck().equals("Y") ? "권태감(피로감)" : "";
- String etcContent = dto.getEtcContentSymptom() == null ? "" : dto.getEtcContentSymptom();
- String ectCheckSymptom = dto.getEtcCheckSymptom().equals("Y") ? "기타(" + etcContent + ")" : "";
-
- String strSymptom = "";
- ArrayList<String> symptom = new ArrayList<String>();
- symptom.add(feverCheck);
- symptom.add(coughCheck);
- symptom.add(colic);
- symptom.add(coldFitCheck);
- symptom.add(sputumCheck);
- symptom.add(ocinCheck);
- symptom.add(chestPain);
- symptom.add(noseCheck);
- symptom.add(vomitingCheck);
- symptom.add(musclePainCheck);
- symptom.add(soreThroatCheck);
- symptom.add(diarrheaCheck);
- symptom.add(headacheCheck);
- symptom.add(dyspneaCheck);
- symptom.add(fatigueCheck);
- symptom.add(ectCheckSymptom);
-
- for (int i = 0; i < symptom.size(); i++) {
- String str = symptom.get(i);
- if (!str.equals("")) {
- strSymptom += str;
- strSymptom += ", ";
- }
- }
-
- strSymptom = strSymptom.trim();
- if (!strSymptom.equals("")) {
- strSymptom = strSymptom.substring(0, strSymptom.length() - 1);
- }
-
-
- return strSymptom;
- }
- }
|