Skip to content

Commit

Permalink
Metadata in import (MID-1980)
Browse files Browse the repository at this point in the history
  • Loading branch information
semancik committed Jan 7, 2015
1 parent bafe40d commit d69921a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
Expand Up @@ -157,6 +157,8 @@ public abstract class SchemaConstants {
public static final String NS_MODEL_CHANNEL = NS_MODEL + "/channels-3";
public static final QName CHANNEL_WEB_SERVICE_QNAME = new QName(NS_MODEL_CHANNEL, "webService");
public static final String CHANNEL_WEB_SERVICE_URI = QNameUtil.qNameToUri(CHANNEL_WEB_SERVICE_QNAME);
public static final QName CHANNEL_OBJECT_IMPORT_QNAME = new QName(NS_MODEL_CHANNEL, "objectImport");
public static final String CHANNEL_OBJECT_IMPORT_URI = QNameUtil.qNameToUri(CHANNEL_OBJECT_IMPORT_QNAME);

public static final String NS_MODEL_SERVICE = NS_MODEL + "/service-3";

Expand Down
Expand Up @@ -174,6 +174,14 @@
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="keepMetadata" type="xsd:boolean" minOccurs="0" default="false">
<xsd:annotation>
<xsd:documentation>
If set to true then the importer will keep the metadata from the source file.
If set to false then the imported will re-generate metadata on each object.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>

Expand Down
Expand Up @@ -15,6 +15,7 @@
*/
package com.evolveum.midpoint.model.impl.importer;

import com.evolveum.midpoint.common.Clock;
import com.evolveum.midpoint.common.crypto.CryptoUtil;
import com.evolveum.midpoint.common.validator.EventHandler;
import com.evolveum.midpoint.common.validator.EventResult;
Expand All @@ -33,8 +34,10 @@
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.prism.query.QueryJaxbConvertor;
import com.evolveum.midpoint.prism.schema.PrismSchema;
import com.evolveum.midpoint.prism.xml.XmlTypeConverter;
import com.evolveum.midpoint.repo.api.RepoAddOptions;
import com.evolveum.midpoint.repo.api.RepositoryService;
import com.evolveum.midpoint.schema.constants.SchemaConstants;
import com.evolveum.midpoint.schema.result.OperationConstants;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.result.OperationResultStatus;
Expand Down Expand Up @@ -108,6 +111,8 @@ public class ObjectImporter {
private RepositoryService repository;
@Autowired(required = true)
private ModelService modelService;
@Autowired(required = true)
private Clock clock;

private Migrator migrator = new Migrator();

Expand Down Expand Up @@ -174,6 +179,17 @@ public <T extends Objectable> EventResult postMarshall(PrismObject<T> prismObjec
opResult.recordFatalError(e);
}
}

if (options == null || (options != null && !BooleanUtils.isTrue(options.isKeepMetadata()))) {
MetadataType metaData = new MetadataType();
String channel = SchemaConstants.CHANNEL_OBJECT_IMPORT_URI;
metaData.setCreateChannel(channel);
metaData.setCreateTimestamp(clock.currentTimeXMLGregorianCalendar());
if (task.getOwner() != null) {
metaData.setCreatorRef(ObjectTypeUtil.createObjectRef(task.getOwner()));
}
object.asObjectable().setMetadata(metaData);
}

objectResult.computeStatus();
if (!objectResult.isAcceptable()) {
Expand Down
Expand Up @@ -21,6 +21,7 @@
import static com.evolveum.midpoint.model.api.ProgressInformation.ActivityType.RESOURCE_OBJECT_OPERATION;
import static com.evolveum.midpoint.model.api.ProgressInformation.StateType.ENTERING;

import com.evolveum.midpoint.common.Clock;
import com.evolveum.midpoint.common.refinery.ResourceShadowDiscriminator;
import com.evolveum.midpoint.model.api.ModelAuthorizationAction;
import com.evolveum.midpoint.model.api.ModelExecuteOptions;
Expand Down Expand Up @@ -132,6 +133,9 @@ public class ChangeExecutor {
@Autowired(required = false)
private WorkflowManager workflowManager;

@Autowired(required = true)
private Clock clock;

private PrismObjectDefinition<UserType> userDefinition = null;
private PrismObjectDefinition<ShadowType> shadowDefinition = null;

Expand Down Expand Up @@ -1036,7 +1040,7 @@ private <T extends ObjectType, F extends ObjectType> void applyMetadata(LensCont
MetadataType metaData = new MetadataType();
String channel = getChannel(context, task);
metaData.setCreateChannel(channel);
metaData.setCreateTimestamp(XmlTypeConverter.createXMLGregorianCalendar(System.currentTimeMillis()));
metaData.setCreateTimestamp(clock.currentTimeXMLGregorianCalendar());
if (task.getOwner() != null) {
metaData.setCreatorRef(ObjectTypeUtil.createObjectRef(task.getOwner()));
}
Expand Down
Expand Up @@ -16,6 +16,7 @@
package com.evolveum.midpoint.model.intest.importer;

import com.evolveum.icf.dummy.resource.DummyResource;
import com.evolveum.midpoint.common.Clock;
import com.evolveum.midpoint.common.InternalsConfig;
import com.evolveum.midpoint.model.api.ModelService;
import com.evolveum.midpoint.model.intest.AbstractConfiguredModelIntegrationTest;
Expand All @@ -25,6 +26,7 @@
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.prism.util.PrismAsserts;
import com.evolveum.midpoint.prism.util.PrismTestUtil;
import com.evolveum.midpoint.prism.xml.XmlTypeConverter;
import com.evolveum.midpoint.prism.xnode.MapXNode;
import com.evolveum.midpoint.prism.xnode.XNode;
import com.evolveum.midpoint.schema.MidPointPrismContextFactory;
Expand All @@ -48,6 +50,7 @@
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.ContextConfiguration;
Expand All @@ -57,6 +60,7 @@
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;

import java.io.File;
Expand Down Expand Up @@ -107,6 +111,9 @@ public class ImportTest extends AbstractConfiguredModelIntegrationTest {
private static String guybrushOid;
private static String hermanOid;

@Autowired
private Clock clock;

@BeforeSuite
public void setup() throws SchemaException, SAXException, IOException {
PrettyPrinter.setDefaultNamespacePrefix(MidPointConstants.NS_MIDPOINT_PUBLIC_PREFIX);
Expand Down Expand Up @@ -146,11 +153,13 @@ public void test001ImportConnector() throws FileNotFoundException, ObjectNotFoun
FileInputStream stream = new FileInputStream(CONNECTOR_DBTABLE_FILE);

dummyAuditService.clear();
XMLGregorianCalendar startTime = clock.currentTimeXMLGregorianCalendar();

// WHEN
modelService.importObjectsFromStream(stream, getDefaultImportOptions(), task, result);

// THEN
XMLGregorianCalendar endTime = clock.currentTimeXMLGregorianCalendar();
result.computeStatus();
display("Result after good import", result);
TestUtil.assertSuccess("Import has failed (result)", result);
Expand All @@ -162,6 +171,8 @@ public void test001ImportConnector() throws FileNotFoundException, ObjectNotFoun
// assertEquals("ICF org.identityconnectors.databasetable.DatabaseTableConnector", connector.getName());
assertEquals(CONNECTOR_NAMESPACE, connector.getNamespace());
assertEquals("org.identityconnectors.databasetable.DatabaseTableConnector", connector.getConnectorType());

assertMetadata(connector, startTime, endTime);

// Check audit
display("Audit", dummyAuditService);
Expand All @@ -173,8 +184,6 @@ public void test001ImportConnector() throws FileNotFoundException, ObjectNotFoun
dummyAuditService.assertExecutionSuccess();
}



@Test
public void test003ImportUsers() throws Exception {
TestUtil.displayTestTile(this,"test003ImportUsers");
Expand All @@ -184,11 +193,13 @@ public void test003ImportUsers() throws Exception {
FileInputStream stream = new FileInputStream(IMPORT_USERS_FILE);

dummyAuditService.clear();
XMLGregorianCalendar startTime = clock.currentTimeXMLGregorianCalendar();

// WHEN
modelService.importObjectsFromStream(stream, getDefaultImportOptions(), task, result);

// THEN
XMLGregorianCalendar endTime = clock.currentTimeXMLGregorianCalendar();
result.computeStatus();
display("Result after good import", result);
TestUtil.assertSuccess("Import has failed (result)", result);
Expand All @@ -206,6 +217,8 @@ public void test003ImportUsers() throws Exception {
assertNull("Arrgh! Pirate sectrets were revealed!",protectedString.getClearValue());
assertNotNull("Er? The pirate sectrets were lost!",protectedString.getEncryptedDataType());

assertMetadata(jack, startTime, endTime);

// Check import with generated OID
// EqualsFilter equal = EqualsFilter.createEqual(UserType.class, PrismTestUtil.getPrismContext(), UserType.F_NAME, "guybrush");
// ObjectQuery query = ObjectQuery.createObjectQuery(equal);
Expand All @@ -222,6 +235,7 @@ public void test003ImportUsers() throws Exception {
PrismAsserts.assertEqualsPolyString("wrong givenName", "Guybrush", guybrush.getGivenName());
PrismAsserts.assertEqualsPolyString("wrong familyName", "Threepwood", guybrush.getFamilyName());
PrismAsserts.assertEqualsPolyString("wrong fullName", "Guybrush Threepwood", guybrush.getFullName());
assertMetadata(guybrush, startTime, endTime);

assertUsers(4);

Expand Down Expand Up @@ -681,4 +695,13 @@ private PrismContainer<Containerable> assertResource(PrismObject<ResourceType> r
return configurationPropertiesContainer;
}

private <O extends ObjectType> void assertMetadata(O objectType, XMLGregorianCalendar startTime, XMLGregorianCalendar endTime) {
MetadataType metadata = objectType.getMetadata();
assertNotNull("No metadata in "+objectType, metadata);
XMLGregorianCalendar createTimestamp = metadata.getCreateTimestamp();
assertNotNull("No createTimestamp in metadata of "+objectType, createTimestamp);
IntegrationTestTools.assertBetween("Wrong createTimestamp in metadata of "+objectType, startTime, endTime, createTimestamp);
assertEquals("Wrong channel in metadata of "+objectType, SchemaConstants.CHANNEL_OBJECT_IMPORT_URI, metadata.getCreateChannel());
}

}

0 comments on commit d69921a

Please sign in to comment.