Skip to content

Commit

Permalink
Merge branch 'master' into dv-6.4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
phantomjinx committed Apr 28, 2017
2 parents 6aa4652 + ff7e27f commit e350cad
Show file tree
Hide file tree
Showing 15 changed files with 244 additions and 28 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 @@ -120,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 @@ -62,6 +62,9 @@ else if (name.equals(ImportExportStatus.DOWNLOADABLE_LABEL)) {
else if (name.equals(ImportExportStatus.CONTENT_LABEL)) {
status.setContent(in.nextString());
}
else if (name.equals(ImportExportStatus.MESSAGE_LABEL)) {
status.setMessage(in.nextString());
}
else if (name.equals(ImportExportStatus.DOWNLOADABLE_SIZE_LABEL)) {
status.setDownloadableSize(in.nextLong());
}
Expand Down Expand Up @@ -101,6 +104,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 @@ -21,6 +21,7 @@
*/
package org.komodo.rest.service;

import static org.komodo.rest.Messages.Error.COMMIT_TIMEOUT;
import static org.komodo.rest.relational.RelationalMessages.Error.DATASERVICE_SERVICE_CREATE_DATASERVICE_ERROR;
import static org.komodo.rest.relational.RelationalMessages.Error.DATASERVICE_SERVICE_DELETE_DATASERVICE_ERROR;
import static org.komodo.rest.relational.RelationalMessages.Error.DATASERVICE_SERVICE_FIND_SOURCE_VDB_ERROR;
Expand All @@ -43,6 +44,7 @@
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
Expand Down Expand Up @@ -74,9 +76,11 @@
import org.komodo.relational.vdb.Vdb;
import org.komodo.relational.workspace.WorkspaceManager;
import org.komodo.repository.ObjectImpl;
import org.komodo.repository.SynchronousCallback;
import org.komodo.rest.KomodoRestException;
import org.komodo.rest.KomodoRestV1Application.V1Constants;
import org.komodo.rest.KomodoService;
import org.komodo.rest.Messages;
import org.komodo.rest.relational.KomodoProperties;
import org.komodo.rest.relational.RelationalMessages;
import org.komodo.rest.relational.connection.RestConnection;
Expand Down Expand Up @@ -518,30 +522,112 @@ public Response cloneDataservice( final @Context HttpHeaders headers,
return createErrorResponseWithForbidden(mediaTypes, RelationalMessages.Error.DATASERVICE_SERVICE_CLONE_SAME_NAME_ERROR, newDataserviceName);
}

UnitOfWork uow = null;
UnitOfWork uow1 = null;
UnitOfWork uow2 = null;

try {
uow = createTransaction(principal, "cloneDataservice", false ); //$NON-NLS-1$
uow1 = createTransaction(principal, "cloneDataservice", false ); //$NON-NLS-1$

// Error if the repo already contains a dataservice with the supplied name.
if ( getWorkspaceManager(uow).hasChild( uow, newDataserviceName ) ) {
if ( getWorkspaceManager(uow1).hasChild( uow1, newDataserviceName ) ) {
return createErrorResponseWithForbidden(mediaTypes, RelationalMessages.Error.DATASERVICE_SERVICE_CLONE_ALREADY_EXISTS);
}

// create new Dataservice
// must be an update
final KomodoObject kobject = getWorkspaceManager(uow).getChild( uow, dataserviceName, DataVirtLexicon.DataService.NODE_TYPE );
final Dataservice oldDataservice = getWorkspaceManager(uow).resolve( uow, kobject, Dataservice.class );

final Dataservice dataservice = getWorkspaceManager(uow).createDataservice( uow, null, newDataserviceName);
oldDataservice.clone(uow, dataservice);
// get the source dataservice
final KomodoObject kobject = getWorkspaceManager(uow1).getChild( uow1, dataserviceName, DataVirtLexicon.DataService.NODE_TYPE );
final Dataservice srcDataservice = getWorkspaceManager(uow1).resolve( uow1, kobject, Dataservice.class );
// Get the service Vdb from the source dataservice
Vdb srcServiceVdb = srcDataservice.getServiceVdb(uow1);

// Create the new dataservice by cloning the source dataservice
final Dataservice newDataservice = getWorkspaceManager(uow1).createDataservice( uow1, null, newDataserviceName);
newDataservice.setServiceVdb(uow1, null);
String tgtServiceVdbName = newDataserviceName+SERVICE_VDB_SUFFIX;
if(getWorkspaceManager(uow1).hasChild(uow1, tgtServiceVdbName, VdbLexicon.Vdb.VIRTUAL_DATABASE)) {
KomodoObject svcVdbObj = getWorkspaceManager(uow1).getChild(uow1, tgtServiceVdbName, VdbLexicon.Vdb.VIRTUAL_DATABASE);
svcVdbObj.remove(uow1);
}

final RestDataservice entity = entityFactory.create(dataservice, uriInfo.getBaseUri(), uow );
final Response response = commit( uow, mediaTypes, entity );
// Create a new service VDB for the target dataservice
KomodoObject tgtServiceVdbObj = getWorkspaceManager(uow1).createVdb(uow1, null, tgtServiceVdbName, tgtServiceVdbName);
// Set owner property on the service vdb
tgtServiceVdbObj.setProperty(uow1, DSB_PROP_OWNER, uow1.getUserName());
Vdb tgtServiceVdb = Vdb.RESOLVER.resolve(uow1, tgtServiceVdbObj);

// Copy source dataservice model info to the new target
Model[] sourceModels = srcServiceVdb.getModels(uow1);
for(Model sourceModel : sourceModels) {
if( sourceModel.getModelType(uow1) == Model.Type.VIRTUAL ) {
// Add to the ServiceVdb a virtual model for the View
Model viewModel = tgtServiceVdb.addModel(uow1, SERVICE_VDB_VIEW_MODEL);
viewModel.setModelType(uow1, Type.VIRTUAL);

// Get source model DDL
byte[] ddlBytes = sourceModel.export(uow1, new Properties());
String viewDdl = new String(ddlBytes);

String srcViewName = dataserviceName+SERVICE_VDB_VIEW_SUFFIX;
String tgtViewName = newDataserviceName+SERVICE_VDB_VIEW_SUFFIX;

viewDdl = viewDdl.replaceFirst(srcViewName, tgtViewName);

// Set DDL on new View Model
viewModel.setModelDefinition(uow1, viewDdl);
} else if ( sourceModel.getModelType(uow1) == Model.Type.PHYSICAL ) {
String modelName = sourceModel.getName(uow1);
// Add model to target
Model targetModel = tgtServiceVdb.addModel(uow1, modelName);
targetModel.setModelType(uow1, Type.PHYSICAL);

// set the model DDL
Properties exportProps = new Properties();
byte[] bytes = sourceModel.export(uow1, exportProps);
String sourceDdl = new String(bytes);
targetModel.setModelDefinition(uow1, sourceDdl);

ModelSource[] srcModelSources = sourceModel.getSources(uow1);
ModelSource srcModelSource = srcModelSources[0];
if(srcModelSource!=null) {
ModelSource tgtModelSource = targetModel.addSource(uow1, srcModelSource.getName(uow1));
tgtModelSource.setJndiName(uow1, srcModelSource.getJndiName(uow1));
tgtModelSource.setTranslatorName(uow1, srcModelSource.getTranslatorName(uow1));
}
}

}

// -------------------------------------------------------------
// Must commit the transaction before setting the service VDB
// -------------------------------------------------------------
final SynchronousCallback callback = ( SynchronousCallback )uow1.getCallback();
uow1.commit();

if ( !callback.await( 30, TimeUnit.SECONDS ) ) {
// callback timeout occurred
String errorMessage = Messages.getString( COMMIT_TIMEOUT, uow1.getName(), 30, TimeUnit.SECONDS );
Object responseEntity = createErrorResponseEntity(mediaTypes, errorMessage);
return Response.status( Status.INTERNAL_SERVER_ERROR )
.entity(responseEntity)
.build();
}

// -------------------------------------------------------------
// Start a new transaction to complete the operation
// -------------------------------------------------------------
uow2 = createTransaction(principal, "cloneDataservice", false ); //$NON-NLS-1$

// Set the service VDB for the cloned dataservice to the new targetServiceVdb
newDataservice.setServiceVdb(uow2, tgtServiceVdb);

final RestDataservice entity = entityFactory.create(newDataservice, uriInfo.getBaseUri(), uow2 );
final Response response = commit( uow2, mediaTypes, entity );
return response;
} catch (final Exception e) {
if ((uow != null) && (uow.getState() != State.ROLLED_BACK)) {
uow.rollback();
if ((uow1 != null) && (uow1.getState() != State.ROLLED_BACK)) {
uow1.rollback();
}
if ((uow2 != null) && (uow2.getState() != State.ROLLED_BACK)) {
uow2.rollback();
}

if (e instanceof KomodoRestException) {
Expand Down
Loading

0 comments on commit e350cad

Please sign in to comment.