Skip to content

Commit

Permalink
Small tweaks/changes for dtgov-120
Browse files Browse the repository at this point in the history
  • Loading branch information
EricWittmann committed Apr 3, 2014
1 parent a57e349 commit da5f6b7
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 108 deletions.
Expand Up @@ -125,6 +125,12 @@ public String getCustomPropertyValue(String propertyName) {
}
}

/**
* Returns all custom properties included in the query result set that begin
* with the given prefix. Note that only properties which have been requested
* via the "propertyName" parameter of the issueing query will be returned.
* @param prefix
*/
public Map<String, String> getPropertiesByPrefix(String prefix) {
Map<String, String> result = new HashMap<String, String>();
if (artifact == null) {
Expand Down
Expand Up @@ -144,12 +144,12 @@ public static boolean isTextDocumentArtifact(DocumentArtifactType artifact) {
}

/**
* Gets the value of one of the s-ramp custom properties.
* Gets all properties with names that begin with the given prefix. This is useful
* if the artifact has a number of custom properties that all start with a common
* domain specific prefix.
*
* @param artifact
* the s-ramp artifact
* @param prefix
* the prefix of the properties searched
* @param artifact the s-ramp artifact
* @param prefix the prefix of the properties searched
* @return the map of custom properties that start by the prefix
*/
public static Map<String, String> getCustomPropertiesByPrefix(BaseArtifactType artifact, String prefix) {
Expand Down
Expand Up @@ -112,7 +112,7 @@ private void registerCommands() {
registry.put(new QName("s-ramp", "showMetaData"), ShowMetaDataCommand.class); //$NON-NLS-1$ //$NON-NLS-2$
registry.put(new QName("s-ramp", "refreshMetaData"), RefreshMetaDataCommand.class); //$NON-NLS-1$ //$NON-NLS-2$
registry.put(new QName("s-ramp", "delete"), DeleteCommand.class); //$NON-NLS-1$ //$NON-NLS-2$
registry.put(new QName("s-ramp", "createArtifact"), CreateArtifactCommand.class); //$NON-NLS-1$ //$NON-NLS-2$
registry.put(new QName("s-ramp", "create"), CreateArtifactCommand.class); //$NON-NLS-1$ //$NON-NLS-2$

// Archive commands
registry.put(new QName("archive", "new"), NewArchiveCommand.class); //$NON-NLS-1$ //$NON-NLS-2$
Expand Down
@@ -1,133 +1,84 @@
/*
* Copyright 2014 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.overlord.sramp.shell.commands.core;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.xml.namespace.QName;

import org.apache.commons.lang.StringUtils;
import org.oasis_open.docs.s_ramp.ns.s_ramp_v1.BaseArtifactEnum;
import org.oasis_open.docs.s_ramp.ns.s_ramp_v1.BaseArtifactType;
import org.overlord.sramp.atom.err.SrampAtomException;
import org.overlord.sramp.client.SrampAtomApiClient;
import org.overlord.sramp.client.SrampClientException;
import org.overlord.sramp.client.SrampClientQuery;
import org.overlord.sramp.client.query.QueryResultSet;
import org.overlord.sramp.common.ArtifactType;
import org.overlord.sramp.shell.BuiltInShellCommand;
import org.overlord.sramp.shell.api.InvalidCommandArgumentException;
import org.overlord.sramp.shell.i18n.Messages;

/**
* This CLI command is used to create a new artifact in the repository. This
* command creates non-Document style artifacts (artifacts with no content).
* It complements the {@link UploadArtifactCommand}, which creates a new
* artifact *with* content.
*
* @author David Virgil Naranjo
*
*/
public class CreateArtifactCommand extends BuiltInShellCommand {

private SrampAtomApiClient client;

private BaseArtifactEnum determineArtifactType(String artifactType) {
return BaseArtifactEnum.EXTENDED_ARTIFACT_TYPE;
/**
* Constructor.
*/
public CreateArtifactCommand() {
}
/* (non-Javadoc)

/**
* @see org.overlord.sramp.shell.api.ShellCommand#execute()
*/
@Override
public boolean execute() throws Exception {
String modeltype = this.requiredArgument(0, Messages.i18n.format("ArtifactModel.Mandatory"));
String name = this.requiredArgument(1, Messages.i18n.format("ArtifactName.Mandatory"));
String description=this.optionalArgument(2);
String artifactTypeArg = this.requiredArgument(0, Messages.i18n.format("ArtifactModel.Mandatory"));
String nameArg = this.requiredArgument(1, Messages.i18n.format("ArtifactName.Mandatory"));
String descriptionArg = this.optionalArgument(2);

String type="";
String model="";
String[] typeArray=modeltype.split("/");
if(typeArray.length==1){
throw new InvalidCommandArgumentException(0, Messages.i18n.format("ArtifactModel.NotCorrectFormat")); //$NON-NLS-1$
}
else{
type = typeArray[typeArray.length - 1];
for(int i=0;i<typeArray.length-1;i++){
model += typeArray[i];
if (i != typeArray.length - 2) {
model += "/";
}
}
}
QName clientVarName = new QName("s-ramp", "client"); //$NON-NLS-1$ //$NON-NLS-2$
client = (SrampAtomApiClient) getContext().getVariable(clientVarName);
if (client == null) {
print(Messages.i18n.format("MissingSRAMPConnection")); //$NON-NLS-1$
return false;
}
if (isUnique(name, modeltype)) {
ArtifactType at = ArtifactType.valueOf(model, type, false);
if (at.isDerived()) {
throw new InvalidCommandArgumentException(0, Messages.i18n.format("ArtifactModel.isDerived")); //$NON-NLS-1$
} else if (at.isDocument()) {
throw new InvalidCommandArgumentException(0, Messages.i18n.format("ArtifactModel.isDocument"));
} else {
BaseArtifactType artifact = at.newArtifactInstance();
artifact.setName(name);
if (StringUtils.isNotBlank(description)) {
artifact.setDescription(description);
}
artifact = client.createArtifact(artifact);
// Put the artifact in the session as the active artifact
QName artifactVarName = new QName("s-ramp", "artifact"); //$NON-NLS-1$ //$NON-NLS-2$
getContext().setVariable(artifactVarName, artifact);
}

ArtifactType artifactType = ArtifactType.valueOf(artifactTypeArg);
if (artifactType.isExtendedType()) {
artifactType = ArtifactType.ExtendedArtifactType(artifactType.getExtendedType(), false);
}

if (artifactType.isDerived()) {
throw new InvalidCommandArgumentException(0, Messages.i18n.format("ArtifactModel.isDerived")); //$NON-NLS-1$
} else if (artifactType.isDocument()) {
throw new InvalidCommandArgumentException(0, Messages.i18n.format("ArtifactModel.isDocument"));
} else {
print(Messages.i18n.format("ArtifactModel.NameAlreadyExistInSramp", name, modeltype)); //$NON-NLS-1$
return false;
BaseArtifactType artifact = artifactType.newArtifactInstance();
artifact.setName(nameArg);
artifact.setDescription(descriptionArg);
artifact = client.createArtifact(artifact);
// Put the artifact in the session as the active artifact
QName artifactVarName = new QName("s-ramp", "artifact"); //$NON-NLS-1$ //$NON-NLS-2$
getContext().setVariable(artifactVarName, artifact);
}


print(Messages.i18n.format("CreateArtifactCommand.Successful", name)); //$NON-NLS-1$
print(Messages.i18n.format("CreateArtifactCommand.Successful", nameArg)); //$NON-NLS-1$
return true;
}

private boolean isUnique(String name, String model) {
int page_size = 20;
StringBuilder queryBuilder = new StringBuilder();
// Initial query

queryBuilder.append("/s-ramp/" + model); //$NON-NLS-1$

List<String> criteria = new ArrayList<String>();
List<Object> params = new ArrayList<Object>();

criteria.add("fn:matches(@name, ?)"); //$NON-NLS-1$
params.add(name.replace("*", ".*")); //$NON-NLS-1$ //$NON-NLS-2$

queryBuilder.append("["); //$NON-NLS-1$
queryBuilder.append(StringUtils.join(criteria, " and ")); //$NON-NLS-1$
queryBuilder.append("]"); //$NON-NLS-1$

// Create the query, and parameterize it
SrampClientQuery query = client.buildQuery(queryBuilder.toString());
for (Object param : params) {
if (param instanceof String) {
query.parameter((String) param);
}
if (param instanceof Calendar) {
query.parameter((Calendar) param);
}
}
QueryResultSet resultSet = null;
try {
resultSet = query.count(page_size + 1).query();
} catch (SrampClientException e) {
print(Messages.i18n.format("MissingSRAMPConnection")); //$NON-NLS-1$
return false;
} catch (SrampAtomException e) {
print(Messages.i18n.format("MissingSRAMPConnection")); //$NON-NLS-1$
return false;
}
if (resultSet.size() == 0) {
return true;
}

return false;
}
}
Expand Up @@ -48,6 +48,8 @@ UpdateMetaDataCommand.usage=s-ramp:updateMetaData
UpdateMetaDataCommand.help=The "updateMetaData" command updates the meta-data of the currently active\nartifact in the context. Whatever changes were made to the active\nartifact will be sent back to the S-RAMP repository.\n\nExample usage\:\n> s-ramp\:updateMetaData
UploadArtifactCommand.usage=s-ramp:upload <pathToArtifactContent> [<artifactType>]
UploadArtifactCommand.help=The "upload" command uploads the content of a local file to\nthe S-RAMP repository, creating a new artifact. The artifact\ntype can optionally be provided. If excluded, the artifact\ntype will be determined based on file extension.\n\nExample usages\:\n> s-ramp\:upload /home/uname/files/mytypes.xsd\n> s-ramp\:upload /home/uname/files/myservice.wsdl WsdlDocument
CreateArtifactCommand.usage=s-ramp:create type name [description]
CreateArtifactCommand.help=The "create" command creates a new artifact in the\nS-RAMP repository, without file content. The artifact\ntype and name must be provided, along with an optional\ndescription.\n\nExample usages\:\n> s-ramp\:create HumanActor bwayne "An actor who also wears a stylish belt."\n> s-ramp\:create MyCustomArtifactType "Name of my artifact"
DeleteOntologyCommand.usage=ontology\:delete <ontologyId>\n\tValid formats for ontologyId\:\n\t feed\:<feedIndex> - an index into the most recent list of ontologies\n\t uuid\:<ontologyUUID> - the UUID of an ontology
DeleteOntologyCommand.help=The "delete" command removes a single ontology from the S-RAMP\nrepository.\n\nExample usage\:\n> ontology\:delete feed\:2\n> ontology\:delete uuid\:2763-2382-39293-382873
ListOntologiesCommand.usage=ontology:list
Expand Down Expand Up @@ -223,9 +225,6 @@ DeployCommand.Failure=FAILED to deploy the artifact.
DeployCommand.FileNotFound=File not found: {0}
ArtifactName.Mandatory=Artifact Name is mandatory.
ArtifactModel.Mandatory=Artifact Model/Type is mandatory.
ArtifactModel.NotCorrectFormat=The Artifact type format is not correct.
ArtifactModel.NameAlreadyExistInSramp=Already exist in s-ramp an artifact with name {0} in the model type {1}. Please introduce an unique name for create a new artifact.
ArtifactModel.isDerived=The artifact you are trying to create is a derived artifact. It is not possible to create derived artifacts with this command.
ArtifactModel.isDocument=The artifact you are trying to create is a document artifact. It is not possible to create document artifacts with this command.
CreateArtifactCommand.usage=createArtifact model/type name [description]
CreateArtifactCommand.Successful=Successfully created the artifact {0}
ArtifactModel.isDerived=The artifact you are trying to create is a derived artifact. It is\nnot possible to create derived artifacts with this command.
ArtifactModel.isDocument=The artifact you are trying to create is a document artifact. It is\nnot possible to create document artifacts with this command.
CreateArtifactCommand.Successful=Successfully created the artifact: {0}

0 comments on commit da5f6b7

Please sign in to comment.