List of usage examples for com.amazonaws.services.simpledb.util SimpleDBUtils decodeZeroPaddingFloat
public static float decodeZeroPaddingFloat(String value)
From source file:util.CacheUtil.java
License:Apache License
public static void loadApp(String aid) { log.debug("Loading app <" + aid + "> into the cache"); Cache appCustomCache = getCacheAppCustoms(); Extra extra = null;/* ww w . ja v a 2 s . c o m*/ List<Ration> rations = null; boolean loaded = false; while (!loaded) { extra = new Extra(); //First we pull the general configuration information SelectRequest request = new SelectRequest( "select `adsOn`, `locationOn`, `fgColor`, `bgColor`, `cycleTime`, `transition` from `" + AdWhirlUtil.DOMAIN_APPS + "` where itemName() = '" + aid + "' limit 1"); try { SelectResult result = sdb.select(request); ; List<Item> itemList = result.getItems(); for (Item item : itemList) { int locationOn = 0; String bgColor = null; String fgColor = null; int cycleTime = 30000; int transition = 0; List<Attribute> attributeList = item.getAttributes(); for (Attribute attribute : attributeList) { try { String attributeName = attribute.getName(); if (attributeName.equals("adsOn")) { int adsOn = SimpleDBUtils.decodeZeroPaddingInt(attribute.getValue()); extra.setAdsOn(adsOn); } else if (attributeName.equals("locationOn")) { locationOn = SimpleDBUtils.decodeZeroPaddingInt(attribute.getValue()); extra.setLocationOn(locationOn); } else if (attributeName.equals("fgColor")) { fgColor = attribute.getValue(); extra.setFgColor(fgColor); } else if (attributeName.equals("bgColor")) { bgColor = attribute.getValue(); extra.setBgColor(bgColor); } else if (attributeName.equals("cycleTime")) { cycleTime = SimpleDBUtils.decodeZeroPaddingInt(attribute.getValue()); extra.setCycleTime(cycleTime); } else if (attributeName.equals("transition")) { transition = SimpleDBUtils.decodeZeroPaddingInt(attribute.getValue()); extra.setTransition(transition); } else { log.warn("SELECT request pulled an unknown attribute <aid: " + aid + " + >: " + attributeName + "|" + attribute.getValue()); } } catch (NumberFormatException e) { log.warn("Invalid data for aid <" + aid + ">: " + e.getMessage(), e); } } //Now we pull the information about the app's nids SelectRequest networksRequest = new SelectRequest("select * from `" + AdWhirlUtil.DOMAIN_NETWORKS + "` where `aid` = '" + aid + "' and deleted is null"); SelectResult networksResult = sdb.select(networksRequest); List<Item> networksList = networksResult.getItems(); rations = new ArrayList<Ration>(); networks_loop: for (Item network : networksList) { String nid = network.getName(); Ration ration = new Ration(nid); List<Attribute> networkAttributeList = network.getAttributes(); for (Attribute networkAttribute : networkAttributeList) { try { String networkAttributeName = networkAttribute.getName(); if (networkAttributeName.equals("adsOn")) { int adsOn = SimpleDBUtils.decodeZeroPaddingInt(networkAttribute.getValue()); if (adsOn == 0) { //We don't care about reporting back a network that isn't active continue networks_loop; } } else if (networkAttributeName.equals("type")) { ration.setType(SimpleDBUtils.decodeZeroPaddingInt(networkAttribute.getValue())); } else if (networkAttributeName.equals("weight")) { ration.setWeight( SimpleDBUtils.decodeZeroPaddingFloat(networkAttribute.getValue())); if (ration.getWeight() < 0) { log.warn("Ration weight should not be less than zero <nid:" + nid + ", weight:" + ration.getWeight() + ">"); } } else if (networkAttributeName.equals("priority")) { ration.setPriority( SimpleDBUtils.decodeZeroPaddingInt(networkAttribute.getValue())); if (ration.getPriority() <= 0) { log.warn("Ration priority should not be less than one <nid:" + nid + ", priority:" + ration.getPriority() + ">"); } } else if (networkAttributeName.equals("key")) { ration.setNetworkKey(networkAttribute.getValue()); } else if (networkAttributeName.equals("aid")) { // We already know the aid. } else if (networkAttributeName.equals("Sdb-item-identifier")) { // Just means it's been edited by SDBExplorer, ignore. } else { log.warn("SELECT request pulled an unknown attribute <nid: " + nid + ">:" + networkAttributeName + "|" + networkAttribute.getValue()); } } catch (NumberFormatException e) { log.warn("Invalid data for <nid: " + nid + ">: ", e); } } if (ration.getType() == AdWhirlUtil.NETWORKS.CUSTOM.ordinal()) { String key = aid; Element cachedAppCustom = appCustomCache.get(key); if (cachedAppCustom != null) { double weight = ration.getWeight(); @SuppressWarnings("unchecked") List<Ration> customRations = (List<Ration>) cachedAppCustom.getObjectValue(); for (Ration customRation : customRations) { customRation.setPriority(ration.getPriority()); double customWeight = AdWhirlUtil.round(customRation.getWeight() * weight / 100, 2); customRation.setWeight(customWeight); rations.add(customRation); } } } else { rations.add(ration); } } } loaded = true; } catch (Exception e) { AdWhirlUtil.logException(e, log); } } try { genJsonConfigs(getCacheConfigs(), aid, extra, rations); } catch (JSONException e) { log.error("Error creating jsonConfig for aid <" + aid + ">: " + e.getMessage(), e); for (Ration ration : rations) { log.warn(ration.toString()); } } }
From source file:util.CacheUtil.java
License:Apache License
public static void loadAppCustom(String aid) { log.debug("Loading app customs for <aid:" + aid + "> into the cache"); List<Ration> rations = null; boolean loaded = false; while (!loaded) { rations = new ArrayList<Ration>(); try {//from w ww.ja v a2s. com String nextToken = null; do { // TODO - remove "is null" from where SelectRequest request = new SelectRequest( "select `weight`, `cid` from `" + AdWhirlUtil.DOMAIN_APP_CUSTOMS + "` where `aid` = '" + aid + "' and `deleted` is null"); request.setNextToken(nextToken); SelectResult result = sdb.select(request); nextToken = result.getNextToken(); List<Item> list = result.getItems(); for (Item item : list) { Ration ration = new Ration(); String acid = item.getName(); List<Attribute> attributeList = item.getAttributes(); for (Attribute attribute : attributeList) { String attributeName = null; try { attributeName = attribute.getName(); if (attributeName.equals("weight")) { double weight = SimpleDBUtils.decodeZeroPaddingFloat(attribute.getValue()); ration.setWeight(weight); } else if (attributeName.equals("cid")) { String cid = attribute.getValue(); ration.setNid(cid); } } catch (NumberFormatException e) { log.error("Invalid data for app custom <acid: " + acid + ">: " + attributeName, e); } } ration.setNetworkKey("__CUSTOM__"); ration.setType(AdWhirlUtil.NETWORKS.CUSTOM.ordinal()); rations.add(ration); } } while (nextToken != null); loaded = true; } catch (Exception e) { AdWhirlUtil.logException(e, log); } } Cache cache = getCacheAppCustoms(); cache.put(new Element(aid, rations)); }
From source file:wwutil.jsoda.DataUtil.java
License:Mozilla Public License
/** Caller should handle custom valueType first before calling this. */ @SuppressWarnings("unchecked") static Object decodeAttrStrToValue(String attrStr, Class valueType) throws Exception { // Set null if input is null. if (attrStr == null) return null; if (valueType == String.class) return attrStr; // Return string type as it is. // non-String field having "" is treated as null. if (attrStr.equals("")) return null; if (valueType == Byte.class || valueType == byte.class) { return new Byte((byte) SimpleDBUtils.decodeZeroPaddingInt(attrStr)); } else if (valueType == Short.class || valueType == short.class) { return new Short((short) SimpleDBUtils.decodeZeroPaddingInt(attrStr)); } else if (valueType == Integer.class || valueType == int.class) { return new Integer(SimpleDBUtils.decodeZeroPaddingInt(attrStr)); } else if (valueType == Long.class || valueType == long.class) { return new Long(SimpleDBUtils.decodeZeroPaddingLong(attrStr)); } else if (valueType == Float.class || valueType == float.class) { return new Float(SimpleDBUtils.decodeZeroPaddingFloat(attrStr)); } else if (valueType == Double.class || valueType == double.class) { return new Double(attrStr); } else if (valueType == Boolean.class || valueType == boolean.class) { return new Boolean(attrStr); } else if (valueType == Character.class || valueType == char.class) { return attrStr.charAt(0); } else if (valueType == Date.class) { return SimpleDBUtils.decodeDate(attrStr); } else if (valueType.isEnum()) { return Enum.valueOf(valueType, attrStr); }//ww w . j a va 2s . co m // de-JSONify the rest. return fromJson(attrStr, valueType); }