Example usage for java.util Map remove

List of usage examples for java.util Map remove

Introduction

In this page you can find the example usage for java.util Map remove.

Prototype

V remove(Object key);

Source Link

Document

Removes the mapping for a key from this map if it is present (optional operation).

Usage

From source file:com.sugaronrest.restapicalls.methodcalls.InsertLinkedEntry.java

/**
 * Updates entry [SugarCRM REST method - set_entry].
 *
 *  @param url REST API Url./*from   ww  w . ja  v  a 2 s.  c o  m*/
 *  @param sessionId Session identifier.
 *  @param moduleName SugarCRM module name.
 *  @param entity The entity object to update.
 *  @param selectFields Selected field list.
 *  @return ReadEntryResponse object.
 */
public static UpdateEntryResponse run(String url, String sessionId, String moduleName, Object entity,
        List<String> selectFields) {

    UpdateEntryResponse updateEntryResponse = null;
    ErrorResponse errorResponse = null;

    String jsonRequest = new String();
    String jsonResponse = new String();

    ObjectMapper mapper = JsonObjectMapper.getMapper();

    try {
        Map<String, Object> requestData = new LinkedHashMap<String, Object>();
        requestData.put("session", sessionId);
        requestData.put("module_name", moduleName);
        Map<String, Object> fields = EntityToNameValueList(entity, selectFields);
        fields.remove("id");
        requestData.put("name_value_list", fields);

        String jsonRequestData = mapper.writeValueAsString(requestData);

        Map<String, Object> request = new LinkedHashMap<String, Object>();
        request.put("method", "set_entry");
        request.put("input_type", "json");
        request.put("response_type", "json");
        request.put("rest_data", requestData);

        jsonRequest = mapper.writeValueAsString(request);

        request.put("rest_data", jsonRequestData);

        HttpResponse response = Unirest.post(url).fields(request).asString();

        if (response == null) {
            updateEntryResponse = new UpdateEntryResponse();
            errorResponse = ErrorResponse.format("An error has occurred!", "No data returned.");
            updateEntryResponse.setStatusCode(HttpStatus.SC_BAD_REQUEST);
            updateEntryResponse.setError(errorResponse);
        } else {

            jsonResponse = response.getBody().toString();

            if (StringUtils.isNotBlank(jsonResponse)) {
                // First check if we have an error
                errorResponse = ErrorResponse.fromJson(jsonResponse);
                if (errorResponse == null) {
                    updateEntryResponse = mapper.readValue(jsonResponse, UpdateEntryResponse.class);
                }
            }

            if (updateEntryResponse == null) {
                updateEntryResponse = new UpdateEntryResponse();
                updateEntryResponse.setError(errorResponse);

                updateEntryResponse.setStatusCode(HttpStatus.SC_OK);
                if (errorResponse != null) {
                    updateEntryResponse.setStatusCode(errorResponse.getStatusCode());
                }
            } else {
                updateEntryResponse.setStatusCode(HttpStatus.SC_OK);
            }
        }
    } catch (Exception exception) {
        updateEntryResponse = new UpdateEntryResponse();
        errorResponse = ErrorResponse.format(exception, exception.getMessage());
        updateEntryResponse.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        errorResponse.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        updateEntryResponse.setError(errorResponse);
    }

    updateEntryResponse.setJsonRawRequest(jsonRequest);
    updateEntryResponse.setJsonRawResponse(jsonResponse);

    return updateEntryResponse;
}

From source file:io.brooklyn.camp.spi.pdp.DeploymentPlan.java

@SuppressWarnings("unchecked")
public static DeploymentPlan of(Map<String, Object> root, String optionalSourceCode) {
    Map<String, Object> attrs = MutableMap.copyOf(root);

    DeploymentPlan result = new DeploymentPlan();
    result.name = (String) attrs.remove("name");
    result.description = (String) attrs.remove("description");
    result.origin = (String) attrs.remove("origin");
    result.sourceCode = optionalSourceCode;
    // TODO version

    result.services = new ArrayList<Service>();
    Object services = attrs.remove("services");
    if (services instanceof Iterable) {
        for (Object service : (Iterable<Object>) services) {
            if (service instanceof Map) {
                result.services.add(Service.of((Map<String, Object>) service));
            } else {
                throw new IllegalArgumentException("service should be map, not " + service.getClass());
            }//ww w . j  av a  2  s . c  o m
        }
    } else if (services != null) {
        // TODO "map" short form
        throw new IllegalArgumentException("artifacts body should be iterable, not " + services.getClass());
    }

    result.artifacts = new ArrayList<Artifact>();
    Object artifacts = attrs.remove("artifacts");
    if (artifacts instanceof Iterable) {
        for (Object artifact : (Iterable<Object>) artifacts) {
            if (artifact instanceof Map) {
                result.artifacts.add(Artifact.of((Map<String, Object>) artifact));
            } else {
                throw new IllegalArgumentException("artifact should be map, not " + artifact.getClass());
            }
        }
    } else if (artifacts != null) {
        // TODO "map" short form
        throw new IllegalArgumentException("artifacts body should be iterable, not " + artifacts.getClass());
    }

    result.customAttributes = attrs;

    return result;
}

From source file:Main.java

public static void rename(Collection<Map<String, Object>> collection, Map<String, String> renames) {
    for (Map<String, Object> map : collection) {
        Object obj;//w ww  .  j a  v  a 2  s.com
        for (String originalName : renames.keySet()) {
            if (map.containsKey(originalName)) {
                obj = map.get(originalName);
                //Must remove first then added after
                map.remove(originalName);
                map.put(renames.get(originalName), obj);
            }
        }

    }
}

From source file:com.zf.util.Post_NetNew.java

public static String pn(Map<String, String> pams) throws Exception {
    if (null == pams) {
        return "";
    }//from   w  w w  . j a va  2 s. c  om
    String strtmp = "url";
    URL url = new URL(pams.get(strtmp));
    pams.remove(strtmp);
    strtmp = "body";
    String body = pams.get(strtmp);
    pams.remove(strtmp);
    strtmp = "POST";
    if (StringUtils.isEmpty(body))
        strtmp = "GET";
    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
    httpConn.setUseCaches(false);
    httpConn.setRequestMethod(strtmp);
    for (String pam : pams.keySet()) {
        httpConn.setRequestProperty(pam, pams.get(pam));
    }
    if ("POST".equals(strtmp)) {
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);
        DataOutputStream dos = new DataOutputStream(httpConn.getOutputStream());
        dos.writeBytes(body);
        dos.flush();
    }
    int resultCode = httpConn.getResponseCode();
    StringBuilder sb = new StringBuilder();
    sb.append(resultCode).append("\n");
    String readLine;
    InputStream stream;
    try {
        stream = httpConn.getInputStream();
    } catch (Exception ignored) {
        stream = httpConn.getErrorStream();
    }
    try {
        BufferedReader responseReader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
        while ((readLine = responseReader.readLine()) != null) {
            sb.append(readLine).append("\n");
        }
    } catch (Exception ignored) {
    }

    return sb.toString();
}

From source file:net.sourceforge.jaulp.io.SerializedObjectUtils.java

/**
 * Gets the changed data./*  w w  w  .  java  2 s  . c o  m*/
 *
 * @param sourceOjbect
 *            the source ojbect
 * @param objectToCompare
 *            the object to compare
 * @return the changed data
 * @throws IllegalAccessException
 *             the illegal access exception
 * @throws InvocationTargetException
 *             the invocation target exception
 * @throws NoSuchMethodException
 *             the no such method exception
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List<SerializedChangedAttributeResult> getChangedData(Object sourceOjbect, Object objectToCompare)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    Map beanDescription = BeanUtils.describe(sourceOjbect);
    beanDescription.remove("class");
    Map clonedBeanDescription = BeanUtils.describe(objectToCompare);
    clonedBeanDescription.remove("class");
    List<SerializedChangedAttributeResult> changedData = new ArrayList<>();
    for (Object key : beanDescription.keySet()) {
        BeanComparator comparator = new BeanComparator(key.toString());
        if (comparator.compare(sourceOjbect, objectToCompare) != 0) {
            Object sourceAttribute = beanDescription.get(key);
            Object changedAttribute = clonedBeanDescription.get(key);
            changedData.add(new SerializedChangedAttributeResult(key, sourceAttribute, changedAttribute));
        }
    }
    return changedData;
}

From source file:net.sourceforge.jaulp.io.SerializedObjectUtils.java

/**
 * Compares the given two objects and gets the changed data.
 *
 * @param sourceOjbect/*from ww w .  java  2 s .com*/
 *            the source ojbect
 * @param objectToCompare
 *            the object to compare
 * @return the changed data
 * @throws IllegalAccessException
 *             the illegal access exception
 * @throws InvocationTargetException
 *             the invocation target exception
 * @throws NoSuchMethodException
 *             the no such method exception
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Map<Object, SerializedChangedAttributeResult> getChangedDataMap(Object sourceOjbect,
        Object objectToCompare)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    Map beanDescription = BeanUtils.describe(sourceOjbect);
    beanDescription.remove("class");
    Map clonedBeanDescription = BeanUtils.describe(objectToCompare);
    clonedBeanDescription.remove("class");
    Map<Object, SerializedChangedAttributeResult> changedData = new HashMap<>();
    for (Object key : beanDescription.keySet()) {
        BeanComparator comparator = new BeanComparator(key.toString());
        if (comparator.compare(sourceOjbect, objectToCompare) != 0) {
            Object sourceAttribute = beanDescription.get(key);
            Object changedAttribute = clonedBeanDescription.get(key);
            changedData.put(key, new SerializedChangedAttributeResult(key, sourceAttribute, changedAttribute));
        }
    }
    return changedData;
}

From source file:hu.petabyte.redflags.engine.util.MappingUtils.java

public static void normalizeMapKeys(Map<String, String> map) {
    Set<String> keys = new LinkedHashSet<String>(map.keySet());
    for (String key : keys) {
        String value = map.get(key);
        map.remove(key);
        key = key.replaceAll("0", ".");
        map.put(key, value);/*from   w  ww  .j a  v a 2s  .  co m*/
    }
}

From source file:de.uniko.west.winter.core.serializing.JSONResultProcessor.java

public static Map<String, Set<Object>> processResultsMultiVarFilter(String results, Set<String> varFilter) {

    try {//from   www  .ja  va 2 s.  c om
        JSONObject jsonResultObject = new JSONObject(results);
        Map<String, Set<Object>> resultsMap = convertJson2Map(jsonResultObject);
        for (String key : resultsMap.keySet()) {
            if (!varFilter.contains(key)) {
                resultsMap.remove(key);
            }
        }
        return resultsMap;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:de.alpharogroup.io.SerializedObjectExtensions.java

/**
 * Gets the changed data.//from  w  w w  . j  a v  a 2  s . com
 *
 * @param sourceOjbect
 *            the source ojbect
 * @param objectToCompare
 *            the object to compare
 * @return the changed data
 * @throws IllegalAccessException
 *             the illegal access exception
 * @throws InvocationTargetException
 *             the invocation target exception
 * @throws NoSuchMethodException
 *             the no such method exception
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List<SerializedChangedAttributeResult> getChangedData(final Object sourceOjbect,
        final Object objectToCompare)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    final Map beanDescription = BeanUtils.describe(sourceOjbect);
    beanDescription.remove("class");
    final Map clonedBeanDescription = BeanUtils.describe(objectToCompare);
    clonedBeanDescription.remove("class");
    final List<SerializedChangedAttributeResult> changedData = new ArrayList<>();
    for (final Object key : beanDescription.keySet()) {
        final BeanComparator comparator = new BeanComparator(key.toString());
        if (comparator.compare(sourceOjbect, objectToCompare) != 0) {
            final Object sourceAttribute = beanDescription.get(key);
            final Object changedAttribute = clonedBeanDescription.get(key);
            changedData.add(new SerializedChangedAttributeResult(key, sourceAttribute, changedAttribute));
        }
    }
    return changedData;
}

From source file:de.alpharogroup.io.SerializedObjectExtensions.java

/**
 * Compares the given two objects and gets the changed data.
 *
 * @param sourceOjbect/*from w ww. ja  v  a2 s  .  c  o m*/
 *            the source ojbect
 * @param objectToCompare
 *            the object to compare
 * @return the changed data
 * @throws IllegalAccessException
 *             the illegal access exception
 * @throws InvocationTargetException
 *             the invocation target exception
 * @throws NoSuchMethodException
 *             the no such method exception
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Map<Object, SerializedChangedAttributeResult> getChangedDataMap(final Object sourceOjbect,
        final Object objectToCompare)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    final Map beanDescription = BeanUtils.describe(sourceOjbect);
    beanDescription.remove("class");
    final Map clonedBeanDescription = BeanUtils.describe(objectToCompare);
    clonedBeanDescription.remove("class");
    final Map<Object, SerializedChangedAttributeResult> changedData = new HashMap<>();
    for (final Object key : beanDescription.keySet()) {
        final BeanComparator comparator = new BeanComparator(key.toString());
        if (comparator.compare(sourceOjbect, objectToCompare) != 0) {
            final Object sourceAttribute = beanDescription.get(key);
            final Object changedAttribute = clonedBeanDescription.get(key);
            changedData.put(key, new SerializedChangedAttributeResult(key, sourceAttribute, changedAttribute));
        }
    }
    return changedData;
}