Skip to content

Commit

Permalink
Merge pull request #485 from mdrillin/TTOOLS-191
Browse files Browse the repository at this point in the history
TEIIDTOOLS-191 allow specification of import options in importer
  • Loading branch information
blafond authored Apr 27, 2017
2 parents ebbd7ef + 09585c2 commit ff7e27f
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ IMPORTER.errorFileNotReadableMsg = The specified File "{0}" is not readable
IMPORTER.errorEmptyMsg = The supplied content string is empty
IMPORTER.ddlDoesNotSupportVDB = The DDL importer does not support importing vdb files. Use the Vdb importer
IMPORTER.vdbOnlySupportsVDB = The Vdb importer only support importing of vdb files.
IMPORTER.nodeExistsReturn = The importer will not continue as node exists and HANDLE_EXISTING_NODE option = 'RETURN'
IMPORTER.nodeExistsReturn = The importer will not import "{0}". The node already exists, and HANDLE_EXISTING_NODE option = 'RETURN'
IMPORTER.nodeExistCreateNew = The importer has found a node named {0} already exists. HANDLE_EXISTING_NODE option = 'CREATE_NEW' so will use the new name {1}
IMPORTER.noNameFailure = No name has been provided.
IMPORTER.newNameFailure = The importer failed to determine a new name from the original name {0}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
import org.modeshape.jcr.api.JcrConstants;
import org.teiid.modeshape.sequencer.dataservice.DataServiceManifest;
import org.teiid.modeshape.sequencer.dataservice.DataServiceManifestReader;
import org.teiid.modeshape.sequencer.dataservice.lexicon.DataVirtLexicon;

/**
* Handles importing and exporting of {@link Dataservice data services}.
Expand Down Expand Up @@ -121,7 +120,7 @@ protected boolean handleExistingNode(UnitOfWork transaction, KomodoObject parent
// RETURN - Return 'false' - do not create a node. Log an error message
case RETURN:
importMessages.addErrorMessage(org.komodo.importer.Messages.getString(
org.komodo.importer.Messages.IMPORTER.nodeExistsReturn));
org.komodo.importer.Messages.IMPORTER.nodeExistsReturn, dsName));
return false;
// CREATE_NEW - Return 'true' - will create a new data service with new unique name. Log a progress message.
case CREATE_NEW:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected void executeImport( UnitOfWork transaction,
switch ( optionValue ) {
case RETURN:
case CREATE_NEW:
importMessages.addErrorMessage( Messages.getString( Messages.IMPORTER.nodeExistsReturn ) );
importMessages.addErrorMessage( Messages.getString( Messages.IMPORTER.nodeExistsReturn, connection.getName() ) );
break;
case OVERWRITE:
shouldSequence = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected boolean handleExistingNode(UnitOfWork transaction,
switch (exNodeOption) {
// RETURN - Return 'false' - do not create a node. Log an error message
case RETURN:
importMessages.addErrorMessage(Messages.getString(Messages.IMPORTER.nodeExistsReturn));
importMessages.addErrorMessage(Messages.getString(Messages.IMPORTER.nodeExistsReturn, vdbName));
return false;
// CREATE_NEW - Return 'true' - will create a new VDB with new unique name. Log a progress message.
case CREATE_NEW:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -941,18 +941,38 @@ private void validateWorkspaceMember( final UnitOfWork uow,
}
}

/**
*
* @param transaction
* the transaction (cannot be <code>null</code> or have a state that is not
* {@link org.komodo.spi.repository.Repository.UnitOfWork.State#NOT_STARTED})
* @param parent the parent of the imported vdb
* @param storageRef the reference to the destination within the storage
* @return the import messages (never <code>null</code>)
* @throws KException if error occurs
*/
public ImportMessages importArtifact(final UnitOfWork transaction,
final KomodoObject parent,
StorageReference storageRef) throws KException {

return importArtifact(transaction,parent,storageRef,new ImportOptions());
}

/**
*
* @param transaction
* the transaction (cannot be <code>null</code> or have a state that is not
* {@link org.komodo.spi.repository.Repository.UnitOfWork.State#NOT_STARTED})
* @param parent the parent of the imported vdb
* @param storageRef the reference to the destination within the storage
* @param importOptions options for the import
* @return the import messages (never <code>null</code>)
* @throws KException if error occurs
*/
public ImportMessages importArtifact(final UnitOfWork transaction,
final KomodoObject parent, StorageReference storageRef) throws KException {
final KomodoObject parent,
StorageReference storageRef,
ImportOptions importOptions) throws KException {
ArgCheck.isNotNull( transaction, "transaction" ); //$NON-NLS-1$
ArgCheck.isTrue( ( transaction.getState() == org.komodo.spi.repository.Repository.UnitOfWork.State.NOT_STARTED ),
"transaction state is not NOT_STARTED" ); //$NON-NLS-1$
Expand All @@ -970,7 +990,6 @@ public ImportMessages importArtifact(final UnitOfWork transaction,
connector = storageService.getConnector(storageRef.getParameters());
stream = connector.read(storageRef.getParameters());

ImportOptions importOptions = new ImportOptions();
ImportMessages importMessages = new ImportMessages();

if (DocumentType.VDB_XML.equals(storageRef.getDocumentType())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ public void testBasicVdbImportCannotCreateVdb() throws Exception {
// Error messages - expect error that the node already exists
List<String> errorMessages = importMessages.getErrorMessages();
assertEquals(1, errorMessages.size());
assertEquals(Messages.getString(Messages.IMPORTER.nodeExistsReturn), errorMessages.get(0));
assertEquals(Messages.getString(Messages.IMPORTER.nodeExistsReturn, TestUtilities.TWEET_EXAMPLE_VDB_NAME), errorMessages.get(0));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ public boolean isEncoded() {
*/
String DOWNLOADABLE_PATH_PROPERTY = "downloadable-path-property";

/**
* Parameter to specify overwrite option for imports
*/
String IMPORT_OVERWRITE_PROPERTY = "import-overwrite-property";

/**
* @return the id of the connector
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ public enum Info {
*/
DRIVER_SUCCESSFULLY_UNDEPLOYED,

/**
* An import export service import success message
*/
IMPORT_EXPORT_SERVICE_IMPORT_SUCCESS_MESSAGE,

/**
* VDB undeployment request sent but not yet undeployed
*/
Expand Down Expand Up @@ -1182,6 +1187,11 @@ public enum Error {
*/
IMPORT_EXPORT_SERVICE_IMPORT_ERROR,

/**
* An import export service import artifact error
*/
IMPORT_EXPORT_SERVICE_IMPORT_ARTIFACT_ERROR,

/**
* An import export service storage types retrieval error
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public ImportExportStatus read( final JsonReader in ) throws IOException {
case ImportExportStatus.CONTENT_LABEL:
status.setContent(in.nextString());
break;
case ImportExportStatus.MESSAGE_LABEL:
status.setMessage(in.nextString());
break;
case ImportExportStatus.DOWNLOADABLE_SIZE_LABEL:
status.setDownloadableSize(in.nextLong());
break;
Expand Down Expand Up @@ -102,6 +105,9 @@ public void write( final JsonWriter out,
out.name(ImportExportStatus.CONTENT_LABEL);
out.value(value.getContent());

out.name(ImportExportStatus.MESSAGE_LABEL);
out.value(value.getMessage());

out.name(ImportExportStatus.DOWNLOADABLE_SIZE_LABEL);
out.value(value.getDownloadableSize());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,18 @@ public class ImportExportStatus implements KRestEntity {
/**
* Label for the size of the downloadable
*/
public static final String DOWNLOADABLE_SIZE_LABEL = "downloadableSize";
public static final String DOWNLOADABLE_SIZE_LABEL = "downloadableSize"; //$NON-NLS-1$

/**
* Label for the content
*/
public static final String CONTENT_LABEL = "content"; //$NON-NLS-1$

/**
* Label for the message
*/
public static final String MESSAGE_LABEL = "message"; //$NON-NLS-1$

/**
* Label for the success flag
*/
Expand All @@ -64,6 +69,8 @@ public class ImportExportStatus implements KRestEntity {

private String content;

private String message;

private boolean success;

private long downloadableSize;
Expand Down Expand Up @@ -106,6 +113,20 @@ public void setContent(String content) {
this.content = content;
}

/**
* @return message if provided or null
*/
public String getMessage() {
return message;
}

/**
* @param message the message
*/
public void setMessage(String message) {
this.message = message;
}

/**
* @return success
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import javax.ws.rs.core.UriInfo;
import org.komodo.core.KEngine;
import org.komodo.importer.ImportMessages;
import org.komodo.importer.ImportOptions;
import org.komodo.importer.ImportOptions.OptionKeys;
import org.komodo.osgi.PluginService;
import org.komodo.rest.KomodoRestException;
import org.komodo.rest.KomodoRestV1Application.V1Constants;
Expand Down Expand Up @@ -392,12 +394,32 @@ public Response importArtifact( final @Context HttpHeaders headers,
parameters,
new DocumentType(sta.getDocumentType()));

ImportMessages messages = getWorkspaceManager(uow).importArtifact(uow, importTarget, storageRef);
if (messages.hasError())
throw new Exception(messages.errorMessagesToString());
// Set desired overwrite setting in options
ImportOptions importOptions = new ImportOptions();
if(parameters.containsKey(StorageConnector.IMPORT_OVERWRITE_PROPERTY)) {
String importOverwrite = parameters.getProperty(StorageConnector.IMPORT_OVERWRITE_PROPERTY);
// RETURN supplied
if( importOverwrite.equals(ImportOptions.ExistingNodeOptions.RETURN.name()) ) {
importOptions.setOption(OptionKeys.HANDLE_EXISTING, ImportOptions.ExistingNodeOptions.RETURN);
// CREATE_NEW supplied
} else if( importOverwrite.equals(ImportOptions.ExistingNodeOptions.CREATE_NEW.name()) ) {
importOptions.setOption(OptionKeys.HANDLE_EXISTING, ImportOptions.ExistingNodeOptions.CREATE_NEW);
// OVERWRITE supplied or no match
} else {
importOptions.setOption(OptionKeys.HANDLE_EXISTING, ImportOptions.ExistingNodeOptions.OVERWRITE);
}
} else {
importOptions.setOption(OptionKeys.HANDLE_EXISTING, ImportOptions.ExistingNodeOptions.OVERWRITE);
}

ImportMessages messages = getWorkspaceManager(uow).importArtifact(uow, importTarget, storageRef, importOptions);
if (messages.hasError()) {
return createErrorResponseWithForbidden(mediaTypes, RelationalMessages.Error.IMPORT_EXPORT_SERVICE_IMPORT_ARTIFACT_ERROR, messages.errorMessagesToString());
}

status.setSuccess(true);
status.setName(storageRef.getRelativeRef());
status.setMessage(RelationalMessages.getString( RelationalMessages.Info.IMPORT_EXPORT_SERVICE_IMPORT_SUCCESS_MESSAGE, importOptions.getOption(OptionKeys.NAME) ));

if(sta.getDocumentType().equals(DocumentType.JAR.toString())) {
String driverName = storageRef.getParameters().getProperty(StorageReference.DRIVER_NAME_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,12 @@ public Response importSampleData(final @Context HttpHeaders headers,
importer.importVdb(uow, sampleStream, workspace, importOptions, importMessages);
uow.commit();

String existingVdbMsg = org.komodo.importer.Messages.getString(
org.komodo.importer.Messages.IMPORTER.nodeExistsReturn);
List<String> errorMsgs = importMessages.getErrorMessages();
if (errorMsgs.isEmpty()) {
msg = RelationalMessages.getString(
RelationalMessages.Error.VDB_SAMPLE_IMPORT_SUCCESS,
sampleName);
} else if (existingVdbMsg.equals(errorMsgs.iterator().next())) {
} else if (errorMsgs.iterator().next().contains("node already exists")) {
msg = RelationalMessages.getString(
RelationalMessages.Error.VDB_SAMPLE_IMPORT_VDB_EXISTS,
sampleName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Info.DATA_SOURCE_SUCCESSFULLY_DEPLOYED = Data source successfully deployed to te
Info.DATA_SOURCE_SUCCESSFULLY_UNDEPLOYED = Data source successfully undeployed from the teiid instance.
Info.DATA_SOURCE_UNDEPLOYMENT_REQUEST_SENT = Data sourve undeployment request sent but cannot yet be verified
Info.DATA_SOURCE_DEPLOYED_WITH_ERRORS = Data source attempted deployment but errors occurred
Info.IMPORT_EXPORT_SERVICE_IMPORT_SUCCESS_MESSAGE = '%s' was imported successfully.
Info.VDB_DEPLOYMENT_STATUS_TITLE = Vdb Deployment Status
Info.VDB_SUCCESSFULLY_DEPLOYED = Vdb successfully deployed to teiid instance
Info.VDB_SUCCESSFULLY_UNDEPLOYED = Vdb successfully undeployed from the teiid instance.
Expand Down Expand Up @@ -254,3 +255,5 @@ Error.IMPORT_EXPORT_SERVICE_EXPORT_ERROR = An error occurred while attempting to
Error.IMPORT_EXPORT_SERVICE_IMPORT_ERROR = An error occurred while attempting to perform an import to storage of type %s: %s
Error.IMPORT_EXPORT_SERVICE_STORAGE_TYPES_ERROR = An error occurred while attempting to retrieve the available import/export storage types: %s
Error.IMPORT_EXPORT_SERVICE_MISSING_PARAMETER_ERROR = The parameter %s is required for the import/export operation but was not specified in the operation call
Error.IMPORT_EXPORT_SERVICE_IMPORT_ARTIFACT_ERROR = An error occurred while attempting to perform the import: %s

0 comments on commit ff7e27f

Please sign in to comment.