/** * Queries all the links for a given mapping, indexed by the source identifier * * <p>This method expects a {@code "links-for-linkType"} defined with a parameter of {@code * "linkType"}. * * @param mapping the mapping to look up the links for * @throws SynchronizationException if the query could not be performed. * @return the mapping from source identifier to the link object for it */ public static Map<String, Link> getLinksForMapping(ObjectMapping mapping, String linkQualifier) throws SynchronizationException { Map<String, Link> sourceIdToLink = new ConcurrentHashMap<String, Link>(); if (mapping != null) { JsonValue query = new JsonValue(new HashMap<String, Object>()); query.put( PARAM_QUERY_FILTER, QueryFilter.and( Arrays.asList( QueryFilter.equalTo("/linkType", mapping.getLinkType().getName()), QueryFilter.equalTo("/linkQualifier", linkQualifier))) .toString()); JsonValue queryResults = linkQuery( mapping.getService().getServerContext(), mapping.getService().getConnectionFactory(), query); for (JsonValue entry : queryResults) { Link link = new Link(mapping); link.fromJsonValue(entry); sourceIdToLink.put(link.sourceId, link); } } return sourceIdToLink; }
/** * Returns a JsonValue containing appropriate identity details * * @param message Description of result * @return The JsonValue Object */ private JsonValue createJsonMessage(String key, Object message) { JsonValue result = new JsonValue(new LinkedHashMap<String, Object>(1)); try { result.put(key, message); return result; } catch (final Exception e) { throw new JsonValueException(result); } }
/** * Expands any interpolation contained within the JsonValue object in-place. * * @param json JsonValue to parse for macros */ public static void expand(JsonValue json) { Iterator<String> iter = json.keys().iterator(); while (iter.hasNext()) { String key = iter.next(); String expanded = parse(json.get(key)); if (expanded != null) { json.put(key, expanded); } } }
/** * TODO: Description. * * @param source * @param target * @param syncOperation the parent {@link ObjectMapping.SyncOperation} instance * @return TODO. * @throws SynchronizationException TODO. */ public Action getAction( LazyObjectAccessor source, LazyObjectAccessor target, final ObjectMapping.SyncOperation syncOperation) throws SynchronizationException { Action result = null; if (action != null) { // static action specified result = action; } else if (script != null) { // action is dynamically determine Map<String, Object> scope = new HashMap<String, Object>(); if (null != scriptScope) { // Make a thread safe copy and put the variables into the scope for (Map.Entry<String, Object> entry : Utils.deepCopy(scriptScope).entrySet()) { if (scope.containsKey(entry.getKey())) { continue; } scope.put(entry.getKey(), entry.getValue()); } } Map<String, Object> recon = new HashMap<String, Object>(); scope.put("recon", recon); JsonValue actionParam = null; if (syncOperation instanceof ObjectMapping.TargetSyncOperation) { actionParam = ((ObjectMapping.TargetSyncOperation) syncOperation).toJsonValue(); } else if (syncOperation instanceof ObjectMapping.SourceSyncOperation) { actionParam = ((ObjectMapping.SourceSyncOperation) syncOperation).toJsonValue(); } if (null != actionParam) { // FIXME Decide if leading underscore should be used here or not actionParam.put("_" + ActionRequest.FIELD_ACTION, "performAction"); recon.put("actionParam", actionParam.getObject()); } scope.put("sourceAction", (syncOperation instanceof ObjectMapping.SourceSyncOperation)); if (source != null) { scope.put("source", source.asMap()); } if (target != null) { scope.put("target", target.asMap()); } try { result = Enum.valueOf(Action.class, script.exec(scope).toString()); } catch (NullPointerException npe) { throw new SynchronizationException("action script returned null value"); } catch (IllegalArgumentException iae) { throw new SynchronizationException("action script returned invalid action"); } catch (ScriptException se) { LOGGER.debug("action script encountered exception", se); throw new SynchronizationException(se); } } return result; }
/** * Queries the links for a match on the second system (links can be bi-directional) * * <p>This method expects a {@code "links-for-targetId"} defined with a parameter of {@code * "targetId"}. * * @param targetId TODO. * @throws SynchronizationException TODO. */ void getLinkFromSecond(String id) throws SynchronizationException { clear(); if (id != null) { JsonValue query = new JsonValue(new HashMap<String, Object>()); query.put( PARAM_QUERY_FILTER, QueryFilter.and( Arrays.asList( QueryFilter.equalTo("/linkType", mapping.getLinkType().getName()), QueryFilter.equalTo("/linkQualifier", linkQualifier), QueryFilter.equalTo("/secondId", id))) .toString()); getLink(query); } }
@Test public void testResource() throws Exception { ScriptName scriptName = new ScriptName("resource", getLanguageName()); ScriptEntry scriptEntry = getScriptRegistry().takeScript(scriptName); Assert.assertNotNull(scriptEntry); Script script = scriptEntry.getScript(new RootContext()); // Set RequestLevel Scope script.put("ketto", 2); script.putSafe("callback", mock(Function.class)); JsonValue createContent = new JsonValue(new LinkedHashMap<String, Object>()); createContent.put("externalId", "701984"); createContent.put("userName", "*****@*****.**"); createContent.put( "assignedDashboard", Arrays.asList("Salesforce", "Google", "ConstantContact")); createContent.put("displayName", "Babs Jensen"); createContent.put("nickName", "Babs"); JsonValue updateContent = createContent.copy(); updateContent.put("_id", UUID.randomUUID().toString()); updateContent.put("profileUrl", "https://login.example.com/bjensen"); final Context context = new ApiInfoContext( new SecurityContext(new RootContext(), "*****@*****.**", null), "", ""); script.put("context", context); CreateRequest createRequest = Requests.newCreateRequest("/Users", "701984", createContent); script.put("createRequest", createRequest); ReadRequest readRequest = Requests.newReadRequest("/Users/701984"); script.put("readRequest", readRequest); UpdateRequest updateRequest = Requests.newUpdateRequest("/Users/701984", updateContent); script.put("updateRequest", updateRequest); PatchRequest patchRequest = Requests.newPatchRequest("/Users/701984", PatchOperation.replace("userName", "ddoe")); script.put("patchRequest", patchRequest); QueryRequest queryRequest = Requests.newQueryRequest("/Users/"); script.put("queryRequest", queryRequest); DeleteRequest deleteRequest = Requests.newDeleteRequest("/Users/701984"); script.put("deleteRequest", deleteRequest); ActionRequest actionRequest = Requests.newActionRequest("/Users", "clear"); script.put("actionRequest", actionRequest); script.eval(); }
private void setConfiguredQueries( Map<String, String> replacements, JsonValue queriesConfig, JsonValue commandsConfig, Map<QueryDefinition, String> defaultQueryMap) { if (queriesConfig == null || queriesConfig.isNull()) { queriesConfig = json(object()); } if (commandsConfig == null || commandsConfig.isNull()) { commandsConfig = json(object()); } // Default query-all-ids to allow bootstrapping of configuration if (!queriesConfig.isDefined(ServerConstants.QUERY_ALL_IDS) && defaultQueryMap != null) { queriesConfig.put( ServerConstants.QUERY_ALL_IDS, defaultQueryMap.get(QueryDefinition.QUERYALLIDS)); } queries.setConfiguredQueries(replacements, queriesConfig); commands.setConfiguredQueries(replacements, commandsConfig); }
/** * Transforms the current instance of this class into a JsonValue object. * * @return JsonValue object of this current instance. */ private JsonValue toJsonValue() { JsonValue jv = new JsonValue(new HashMap<String, Object>()); sourceId = mapping.getLinkType().normalizeSourceId(sourceId); targetId = mapping.getLinkType().normalizeTargetId(targetId); jv.put("linkType", mapping.getLinkType().getName()); jv.put("linkQualifier", linkQualifier); if (mapping.getLinkType().useReverse()) { jv.put("secondId", sourceId); jv.put("firstId", targetId); } else { jv.put("firstId", sourceId); jv.put("secondId", targetId); } return jv; }