/** * Create a new {@code Component} as a copy from a existing component with a specific id. <br> * <b>Use with care!</b> * <p> * This factory is to be used by {@link org.dswarm.persistence.model.job.utils.TransformationDeserializer} to avoid reflection * based access to a private/protected field, since the Json deserializer needs a way to set the id that was provided by the * JSON. * </p> * <p> * The id is otherwise assigned by the database/Hibernate layer. You should never need this outside of * {@code TransformationDeserializer}. * </p> * * @param component the base component that will be copied * @param uuid the target component's id value * @return a new component with the given id and all other attributes copied from the provided component. */ public static Component withId(final Component component, final String uuid) { final Component newComponent = new Component(uuid); newComponent.setFunction(component.getFunction()); newComponent.setInputComponents(component.getInputComponents()); newComponent.setOutputComponents(component.getOutputComponents()); newComponent.setParameterMappings(component.getParameterMappings()); newComponent.setDescription(component.getDescription()); newComponent.setName(component.getName()); return newComponent; } }
private Element convertIfElseFunction(final Component multipleInputComponent, final String collectionNameAttribute, final Element collection) throws DMPConverterException { final Map<String, String> parameters = multipleInputComponent.getParameterMappings(); if (parameters == null) { throw new DMPConverterException("cannot convert if/else component; there are not parameter mappings"); } if (!parameters.containsKey(IF_VARIABLE_IDENTIFIER)) { throw new DMPConverterException("cannot convert if/else component; cannot find parameter mapping for if-branch in parameter mappings"); } final String ifBranchComponentVariableName = parameters.get(IF_VARIABLE_IDENTIFIER); if (!parameters.containsKey(ELSE_VARIABLE_IDENTIFIER)) { throw new DMPConverterException("cannot convert if/else component; cannot find parameter mapping for else-branch in parameter mappings"); } final String elseBranchComponentVariableName = parameters.get(ELSE_VARIABLE_IDENTIFIER); final String ifElseComponentID = multipleInputComponent.getUuid(); final String ifElseComponentName = IF_ELSE_COMPONENT_NAME_PREFIX + ifElseComponentID; collection.setAttribute(METAMORPH_DATA_TARGET, MF_VARIABLE_PREFIX + collectionNameAttribute); createDataElement(collection, ifBranchComponentVariableName, ifElseComponentName + IF_BRANCH_POSTFIX, Optional.empty()); createDataElement(collection, elseBranchComponentVariableName, ifElseComponentName + ELSE_BRANCH_POSTFIX, Optional.empty()); return collection; }
object.setFunction(objectWithUpdates.getFunction()); object.setParameterMappings(objectWithUpdates.getParameterMappings()); final Set<Component> inputComponents = objectWithUpdates.getInputComponents(); Set<Component> newInputComponents; inputComponent.removeOutputComponent(objectWithUpdates); inputComponent.addOutputComponent(object); object.setInputComponents(newInputComponents); final Set<Component> outputComponents = objectWithUpdates.getOutputComponents(); Set<Component> newOutputComponents; outputComponent.removeInputComponent(objectWithUpdates); outputComponent.addInputComponent(object); object.setOutputComponents(newOutputComponents);
/** * {@inheritDoc} */ @Override public void updateObjectInternal(final Component object, final Component updateObject) throws DMPPersistenceException { super.updateObjectInternal(object, updateObject); final Function function = object.getFunction(); final Set<Component> inputComponents = object.getInputComponents(); final Set<Component> outputComponents = object.getOutputComponents(); final Map<String, String> parameterMappings = object.getParameterMappings(); // final Transformation transformation = object.getTransformation(); updateObject.setFunction(function); updateObject.setInputComponents(inputComponents); updateObject.setOutputComponents(outputComponents); updateObject.setParameterMappings(parameterMappings); // updateObject.setTransformation(transformation); }
@Override public boolean completeEquals(final Object obj) { return Component.class.isInstance(obj) && super.completeEquals(obj) && Objects.equal(((Component) obj).getParameterMappings(), getParameterMappings()) && DMPPersistenceUtil.getFunctionUtils().completeEquals(((Component) obj).getFunction(), getFunction()) && Objects.equal(((Component) obj).getInputComponents(), getInputComponents()) && Objects.equal(((Component) obj).getOutputComponents(), getOutputComponents()); }
final Component component = new Component(componentUUID); component.setName(componentName); component.setFunction(function); component.setParameterMappings(parameterMapping); Assert.assertNotNull("the component name shouldn't be null", component.getName()); Assert.assertEquals("the component names are not equal", componentName, component.getName()); Assert.assertNotNull("the component parameter mappings shouldn't be null", component.getParameterMappings()); Assert.assertEquals("the component parameter mappings' size are not equal", 1, component.getParameterMappings().size()); Assert.assertTrue("the component parameter mappings doesn't contain a mapping for function parameter '" + functionParameterName + "'", component.getParameterMappings().containsKey(functionParameterName)); Assert.assertEquals("the component parameter mapping for '" + functionParameterName + "' are not equal", componentVariableName, component .getParameterMappings().get(functionParameterName));
final Component component1 = new Component(componentUUID); component1.setName(component1Name); component1.setFunction(function1); component1.setParameterMappings(parameterMapping1); final Component component2 = new Component(component2UUID); component2.setName(component2Name); component2.setFunction(function2); component2.setParameterMappings(parameterMapping2); final Component component = new Component(component3UUID); component.setName(componentName); component.setFunction(function); component.setParameterMappings(parameterMapping); component.setInputComponents(inputComponents); component.setOutputComponents(outputComponents); Assert.assertNotNull("the component name shouldn't be null", component.getName()); Assert.assertEquals("the component names are not equal", componentName, component.getName()); Assert.assertNotNull("the component parameter mappings shouldn't be null", component.getParameterMappings()); Assert.assertEquals("the component parameter mappings' size are not equal", 1, component.getParameterMappings().size()); Assert.assertTrue("the component parameter mappings doesn't contain a mapping for function parameter '" + functionParameterName + "'", component.getParameterMappings().containsKey(functionParameterName)); Assert.assertEquals("the component parameter mapping for '" + functionParameterName + "' are not equal", componentVariableName, component .getParameterMappings().get(functionParameterName)); Assert.assertNotNull("the component input components set shouldn't be null", component.getInputComponents());
final Component component1 = new Component(component1UUID); component1.setName(component1Name); component1.setFunction(function1); component1.setParameterMappings(parameterMapping1); final Component component2 = new Component(component2UUID); component2.setName(component2Name); component2.setFunction(function2); component2.setParameterMappings(parameterMapping2); final Component component = new Component(componentUUID); component.setName(componentName); component.setFunction(function); component.setParameterMappings(parameterMapping); component.setInputComponents(inputComponents); component.setOutputComponents(outputComponents); final Component transformationComponent = new Component(transformationComponentId); transformationComponent.setName("prepare first name"); transformationComponent.setFunction(transformation); transformationComponent.setParameterMappings(transformationComponentParameterMappings); final Component transformationComponent2 = new Component(transformationComponentId2); transformationComponent2.setName("prepare family name"); transformationComponent2.setFunction(transformation); transformationComponent2.setParameterMappings(transformationComponentParameterMappings2); final Component component4 = new Component(component4UUID); component4.setName(component4Name);
public Component createComponent(final String name, final Map<String, String> parameterMappings, final Function function, final Set<Component> inputComponents, final Set<Component> outputComponents) throws Exception { // component needs to be a persistent object from the beginning final Component component = createObject().getObject(); component.setName(name); component.setFunction(function); component.setParameterMappings(parameterMappings); if (inputComponents != null) { component.setInputComponents(inputComponents); } if (outputComponents != null) { component.setOutputComponents(outputComponents); } // update method needs to be utilised here, because component was already created final Component updatedComponent = updateAndCompareObject(component, component); Assert.assertNotNull("the updated component shouldn't be null", updatedComponent); Assert.assertNotNull("the component name shouldn't be null", updatedComponent.getUuid()); return updatedComponent; }
final String funtionName = component.getFunction().getName(); param.setValue(component.getName()); componentElement.setAttributeNode(param); final Map<String, String> parameterMappings = component.getParameterMappings();
@Test public void deserializeNestedTransformationTest() throws IOException { final CharSource source = Resources.asCharSource(Resources.getResource("deser-nested-transformation.json"), Charset.forName("UTF-8")); final Mapping mapping = objectMapper.readValue(source.openStream(), Mapping.class); final Component transformationComponent = mapping.getTransformation(); MatcherAssert.assertThat(transformationComponent.getName(), Matchers.equalTo("transformation")); MatcherAssert.assertThat(transformationComponent.getDescription(), Matchers.equalTo("transformation")); MatcherAssert.assertThat(transformationComponent.getParameterMappings(), Matchers.allOf(Matchers.hasKey("dataset"), Matchers.hasKey("variable_name"), Matchers.hasKey("transformationOutputVariable"))); MatcherAssert.assertThat( transformationComponent.getParameterMappings(), Matchers.allOf(Matchers.hasValue("http://data.slub-dresden.de/resources/1/schema#dataset"), Matchers.hasValue("http://data.slub-dresden.de/resources/1/schema#variable_name"), Matchers.hasValue("http://purl.org/dc/elements/1.1/title"))); CustomTransformationDeserializerTest.testTransformation((Transformation) transformationComponent.getFunction()); }
@Test public void testSqlMapSTask() throws Exception { final Task task = getTask("dd-1386.task.json"); final Job job = task.getJob(); final Set<Mapping> mappings = job.getMappings(); final Mapping mapping = mappings.iterator().next(); final Component mappingTransformationComponent = mapping.getTransformation(); final Transformation mappingTransformationComponentFunction = (Transformation) mappingTransformationComponent.getFunction(); final Set<Component> mappingTransformationComponentFunctionComponents = mappingTransformationComponentFunction.getComponents(); final Iterator<Component> iterator = mappingTransformationComponentFunctionComponents.iterator(); final String user = readManuallyFromTypeSafeConfig("dswarm.db.metadata.username"); final String pass = readManuallyFromTypeSafeConfig("dswarm.db.metadata.password"); final String db = readManuallyFromTypeSafeConfig("dswarm.db.metadata.schema"); while(iterator.hasNext()) { final Component component = iterator.next(); if("sqlmap".equals(component.getFunction().getName())) { final Map<String, String> sqlMapLookupParameterMappings = component.getParameterMappings(); setSqlMapParameterMappings(sqlMapLookupParameterMappings, user, pass, db); } } testXMLTaskWithTuples("dd-1386.task.result.json", "dd-1386.marcxml.tuples.json", task); }
@SafeVarargs private static Component checkComponent(final Transformation transformation, final String uuid, final String name, final String description, final List<String> functionParameters, final Tuple<String, String>... parameterMappings) { // TODO: changed Configuration.class to Component.class (y Configuration.class here - these are components!) final List<Component> configurations = Lambda.filter(Lambda.having(Lambda.on(Component.class).getUuid(), Matchers.equalTo(uuid)), transformation.getComponents()); MatcherAssert.assertThat(configurations, Matchers.hasSize(1)); final Component component = configurations.get(0); MatcherAssert.assertThat(component.getUuid(), Matchers.equalTo(uuid)); MatcherAssert.assertThat(component.getName(), Matchers.equalTo(name)); MatcherAssert.assertThat(component.getDescription(), Matchers.equalTo(description)); final Map<String, String> componentParameterMappings = component.getParameterMappings(); for (final Tuple<String, String> parameterMapping : parameterMappings) { final String key = parameterMapping.v1(); final String value = parameterMapping.v2(); MatcherAssert.assertThat(componentParameterMappings, Matchers.hasKey(key)); MatcherAssert.assertThat(componentParameterMappings, Matchers.hasValue(value)); } CustomTransformationDeserializerTest.checkFunction(component, uuid, name, description, functionParameters.toArray(new String[functionParameters.size()])); return component; }
final String functionName = component.getFunction().getName(); final Map<String, String> componentParameterMapping = component.getParameterMappings(); if (component.getOutputComponents() == null || component.getOutputComponents().isEmpty()) { if (component.getOutputComponents() == null || component.getOutputComponents().isEmpty()) {
private static String getComponentFunctionName(final Component component) { final String componentFunctionName = component.getFunction().getName(); if (METAMORPH_FUNCTION_SQLMAP.equals(componentFunctionName)) { return METAMORPH_FUNCTION_LOOKUP; } return componentFunctionName; }
/** * note: result will be cache (temporarily - for re-utilisation in a test) * * @return * @throws Exception */ @Override public Transformation createAndPersistDefaultCompleteObject() throws Exception { final String transformationName = "my transformation"; if (!cache.containsKey(transformationName)) { final String transformationDescription = "transformation which just makes use of one function"; final String transformationParameter = "transformationInputString"; final Component component = componentServiceTestUtils.createAndPersistDefaultCompleteObject(); final Set<Component> components = Sets.newLinkedHashSet(); components.add(component.getInputComponents().iterator().next()); components.add(component); components.add(component.getOutputComponents().iterator().next()); final LinkedList<String> parameters = Lists.newLinkedList(); parameters.add(transformationParameter); final Transformation transformation = createTransformation(transformationName, transformationDescription, components, parameters); cache.put(transformationName, transformation); } return cache.get(transformationName); }
final Set<String> collectionSourceAttributes) { final Map<String, String> parameters = multipleInputComponent.getParameterMappings(); multipleInputComponent.setParameterMappings(extendedParameterMappings);
private void replaceFunctionsInComponent(final Component component) throws DMPPersistenceException { if (component == null) { return; } final Function existingComponentFunction = component.getFunction(); final FunctionType componentFunctionFunctionType = existingComponentFunction.getFunctionType(); switch (componentFunctionFunctionType) { case Function: final Function persistentFunction = replaceFunction(existingComponentFunction); component.setFunction(persistentFunction); break; case Transformation: replaceFunctionsInTransformation((Transformation) existingComponentFunction); break; } }
private static List<String> getParameterMappingKeys(final String variableName, final Component transformationComponent) { List<String> parameterMappingKeys = null; final Map<String, String> transformationParameterMapping = transformationComponent.getParameterMappings(); for (final Entry<String, String> parameterMapping : transformationParameterMapping.entrySet()) { if (StringEscapeUtils.unescapeXml(parameterMapping.getValue()).equals(variableName)) { if (parameterMappingKeys == null) { parameterMappingKeys = new ArrayList<>(); } parameterMappingKeys.add(parameterMapping.getKey()); } } return parameterMappingKeys; }
/** * sort components re. their natural order (begin from the end, i.e., component without output components) * * @param components original components set * @return */ private static javaslang.collection.List<Component> sortComponents(final Set<Component> components) { final Optional<Component> optionalLastComponent = components.stream() .filter(component -> { final Set<Component> outputComponents = component.getOutputComponents(); return outputComponents == null || outputComponents.isEmpty(); }) .findFirst(); if (!optionalLastComponent.isPresent()) { return javaslang.collection.List.ofAll(components); } final Component lastComponent = optionalLastComponent.get(); final javaslang.collection.List<Component> lastComponentList = javaslang.collection.List.of(lastComponent); final javaslang.collection.Map<String, Component> componentMap = javaslang.collection.HashSet.ofAll(components) .toMap(component -> Tuple.of(component.getUuid(), component)); final javaslang.collection.List<Component> emptyComponentList = javaslang.collection.List.empty(); return addComponents(lastComponentList, componentMap, emptyComponentList); }