Example usage for com.amazonaws.services.simpledb.util SimpleDBUtils decodeZeroPaddingInt

List of usage examples for com.amazonaws.services.simpledb.util SimpleDBUtils decodeZeroPaddingInt

Introduction

In this page you can find the example usage for com.amazonaws.services.simpledb.util SimpleDBUtils decodeZeroPaddingInt.

Prototype

public static int decodeZeroPaddingInt(String value) 

Source Link

Document

Decodes zero-padded positive integer value from the string representation

Usage

From source file:com.aipo.aws.simpledb.ResultItem.java

License:Open Source License

public Integer getAsInt(String key) {
    String value = get(key);/*w w w.  j  a v a 2  s. co m*/
    if (value == null) {
        return null;
    }
    return SimpleDBUtils.decodeZeroPaddingInt(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 .j a va 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 loadCustom(String nid) {
    log.debug("Loading custom <" + nid + "> into the cache");

    CustomAd customAd = null;/* w  ww.j  av a 2s.c  om*/

    boolean loaded = false;
    while (!loaded) {
        //Custom (house) ad select query
        SelectRequest customRequest = new SelectRequest(
                "select * from `" + AdWhirlUtil.DOMAIN_CUSTOMS + "` where itemName() = '" + nid + "' limit 1");
        try {
            SelectResult customResult = sdb.select(customRequest);
            List<Item> customList = customResult.getItems();

            for (Item cusItem : customList) {
                customAd = new CustomAd(cusItem.getName());

                List<Attribute> cusAttributeList = cusItem.getAttributes();
                for (Attribute attribute : cusAttributeList) {
                    try {
                        String attributeName = attribute.getName();
                        if (attributeName.equals("type")) {
                            customAd.setType(SimpleDBUtils.decodeZeroPaddingInt(attribute.getValue()));
                        } else if (attributeName.equals("aid")) {
                            // Legacy field, shouldn't be used
                        } else if (attributeName.equals("imageLink")) {
                            customAd.setImageLink(attribute.getValue());
                        } else if (attributeName.equals("link")) {
                            customAd.setLink(attribute.getValue());
                        } else if (attributeName.equals("description")) {
                            customAd.setDescription(attribute.getValue());
                        } else if (attributeName.equals("name")) {
                            customAd.setName(attribute.getValue());
                        } else if (attributeName.equals("linkType")) {
                            customAd.setLinkType(attribute.getValue());
                        } else if (attributeName.equals("launchType")) {
                            customAd.setLaunchType(attribute.getValue());
                        } else if (attributeName.equals("uid")) {
                            // We don't care who this belongs to
                        } else if (attributeName.equals("deleted")) {
                            // We don't care when it was deleted
                        } else if (attributeName.equals("Sdb-item-identifier")) {
                            // Just means it's been edited by SDBExplorer, ignore.
                        } else {
                            log.warn("SELECT request pulled an unknown attribute <cid: " + nid + ">: "
                                    + attributeName + "|" + attribute.getValue());
                        }
                    } catch (NumberFormatException e) {
                        log.warn("Invalid data for custom <" + nid + ">: " + e.getMessage(), e);
                    }
                }
            }

            loaded = true;
        } catch (Exception e) {
            AdWhirlUtil.logException(e, log);
        }
    }

    try {
        genJsonCustoms(getCacheCustoms(), nid, customAd);
    } catch (JSONException e) {
        log.error("Error creating jsonCustom: " + e.getMessage());
        return;
    }
}

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);
    }/*from   w ww  . j a  va2s. com*/

    // de-JSONify the rest.
    return fromJson(attrStr, valueType);
}