package org.xmx0632.deliciousfruit.api.v1.helper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.xmx0632.deliciousfruit.api.v1.UserAccountApiController; public class CategoryToClient { private static Logger log = LoggerFactory .getLogger(UserAccountApiController.class); private final static String CATEGORY_ID_PREFIX = "C"; private final static String SUB_CATEGORY_ID_PREFIX = "S"; private final static int LENGTH_CLIENT_ID = 6; public static String formatCategoryID(long id) { return CATEGORY_ID_PREFIX + String.format("%1$0" + LENGTH_CLIENT_ID + "d", id); } public static String formatSubCategoryID(long id) { return SUB_CATEGORY_ID_PREFIX + String.format("%1$0" + LENGTH_CLIENT_ID + "d", id); } public static boolean needSubCategoryID(long id, String idsNeedSub) { boolean isNeed = false; if (idsNeedSub == null || "".equals(idsNeedSub)) { return isNeed; } String ids[] = idsNeedSub.split(","); for (String idNeedSub : ids) { if (idNeedSub == null || "".equals(idNeedSub.trim())) { continue; } long idLong = 0; try { idLong = Long.parseLong(idNeedSub.trim()); } catch (Exception e) { log.error("Invalid Category ID from Config", e); } if (idLong == id) { isNeed = true; break; } } return isNeed; } public static boolean isCategory(String fromatedID) { if (fromatedID != null && fromatedID.startsWith(CATEGORY_ID_PREFIX)) { return true; } return false; } public static boolean isSubCategory(String fromatedID) { if (fromatedID != null && fromatedID.startsWith(SUB_CATEGORY_ID_PREFIX)) { return true; } return false; } public static long getCategoryID(String fromatedID) { long id = 0; if (fromatedID == null || "".equals(fromatedID)) { return id; } try { id = Long.parseLong(fromatedID.substring(1, fromatedID.length())); } catch (Exception e) { log.error("Invalid formated Category ID", e); } return id; } public static long getSubCategoryID(String fromatedID) { long id = 0; if (fromatedID == null || "".equals(fromatedID)) { return id; } try { id = Long.parseLong(fromatedID.substring(1, fromatedID.length())); } catch (Exception e) { log.error("Invalid formated Sub Category ID", e); } return id; } }