Skip to content

Commit

Permalink
Merge pull request #86 from elvisisking/repo-query
Browse files Browse the repository at this point in the history
Added a query method to the repository manager. Added i18n of methods in Precondition. Added tests for query and moved some tests from deriver test into repository manager test.
  • Loading branch information
elvisisking committed Jan 21, 2013
2 parents a7ff4b2 + 4dd93a4 commit 1e8e252
Show file tree
Hide file tree
Showing 11 changed files with 359 additions and 108 deletions.
8 changes: 8 additions & 0 deletions komodo-common/src/main/java/org/komodo/common/CommonI18n.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ public class CommonI18n extends I18n {
public static String problemLoadingI18nClass;
public static String problemLoadingI18nProperties;

public static String objectIsNotInstanceOf;
public static String objectIsNotNull;
public static String stringsDoNotMatchExactly;
public static String collectionIsEmpty;
public static String mapIsEmpty;
public static String stringIsEmpty;
public static String objectIsNull;

static {
final CommonI18n i18n = new CommonI18n();
i18n.initialize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
*/
package org.komodo.common.util;

import java.text.MessageFormat;
import java.util.Collection;
import java.util.Map;
import org.komodo.common.CommonI18n;
import org.komodo.common.i18n.I18n;

/**
* Utilities useful in testing method preconditions.
Expand All @@ -20,14 +23,14 @@ public class Precondition {
* @param requiredClass the class the object must be an <code>instanceof</code> (cannot be <code>null</code>)
*/
public static void instanceOf(final Object objectBeingChecked,
String identifier,
final String identifier,
final Class requiredClass) {
assert !StringUtil.isEmpty(identifier) : "identifier cannot be empty"; //$NON-NLS-1$
Precondition.notNull(objectBeingChecked, "objectBeingChecked"); //$NON-NLS-1$
Precondition.notNull(requiredClass, "requiredClass"); //$NON-NLS-1$

if (!requiredClass.isInstance(objectBeingChecked)) {
throw new IllegalArgumentException(MessageFormat.format("\"{0}\" is not null", identifier)); //$NON-NLS-1$
throw new IllegalArgumentException(I18n.bind(CommonI18n.objectIsNotInstanceOf, identifier, requiredClass.getName()));
}
}

Expand All @@ -37,11 +40,11 @@ public static void instanceOf(final Object objectBeingChecked,
* @throws IllegalArgumentException if the object is <em>not</em> <code>null</code>
*/
public static void isNull(final Object objectBeingChecked,
String identifier) {
final String identifier) {
assert !StringUtil.isEmpty(identifier) : "identifier cannot be empty"; //$NON-NLS-1$

if (objectBeingChecked != null) {
throw new IllegalArgumentException(MessageFormat.format("\"{0}\" is not null", identifier)); //$NON-NLS-1$
throw new IllegalArgumentException(I18n.bind(CommonI18n.objectIsNotNull, identifier));
}
}

Expand All @@ -66,7 +69,35 @@ public static void matchesExactly(final String actual,
}

if (!matches) {
throw new IllegalArgumentException(MessageFormat.format("\"{0}\" does not exactly match", identifier)); //$NON-NLS-1$
throw new IllegalArgumentException(I18n.bind(CommonI18n.stringsDoNotMatchExactly, identifier, actual, expected));
}
}

/**
* @param collectionBeingChecked the collection being checked (can be <code>null</code> or empty)
* @param identifier the identifier used in the error message (cannot be <code>null</code> or empty)
* @throws IllegalArgumentException if the collection is <code>null</code> or empty
*/
public static void notEmpty(final Collection<?> collectionBeingChecked,
final String identifier) {
assert !StringUtil.isEmpty(identifier) : "identifier cannot be empty"; //$NON-NLS-1$

if (CollectionUtil.isEmpty(collectionBeingChecked)) {
throw new IllegalArgumentException(I18n.bind(CommonI18n.collectionIsEmpty, identifier));
}
}

/**
* @param mapBeingChecked the map being checked (can be <code>null</code> or empty)
* @param identifier the identifier used in the error message (cannot be <code>null</code> or empty)
* @throws IllegalArgumentException if the collection is <code>null</code> or empty
*/
public static void notEmpty(final Map<?, ?> mapBeingChecked,
final String identifier) {
assert !StringUtil.isEmpty(identifier) : "identifier cannot be empty"; //$NON-NLS-1$

if (CollectionUtil.isEmpty(mapBeingChecked)) {
throw new IllegalArgumentException(I18n.bind(CommonI18n.mapIsEmpty, identifier));
}
}

Expand All @@ -76,11 +107,11 @@ public static void matchesExactly(final String actual,
* @throws IllegalArgumentException if the text is <code>null</code> or empty
*/
public static void notEmpty(final String textBeingChecked,
String identifier) {
final String identifier) {
assert !StringUtil.isEmpty(identifier) : "identifier cannot be empty"; //$NON-NLS-1$

if (StringUtil.isEmpty(textBeingChecked)) {
throw new IllegalArgumentException(MessageFormat.format("\"{0}\" is empty", identifier)); //$NON-NLS-1$
throw new IllegalArgumentException(I18n.bind(CommonI18n.stringIsEmpty, identifier));
}
}

Expand All @@ -90,11 +121,11 @@ public static void notEmpty(final String textBeingChecked,
* @throws IllegalArgumentException if the object is <code>null</code>
*/
public static void notNull(final Object objectBeingChecked,
String identifier) {
final String identifier) {
assert !StringUtil.isEmpty(identifier) : "identifier cannot be empty"; //$NON-NLS-1$

if (objectBeingChecked == null) {
throw new IllegalArgumentException(MessageFormat.format("\"{0}\" is null", identifier)); //$NON-NLS-1$
throw new IllegalArgumentException(I18n.bind(CommonI18n.objectIsNull, identifier));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,15 @@ problemLoadingI18nClass = There was a problem loading I18n '%s' field values

# 1 = class name of I18n subclass
problemLoadingI18nProperties = There was a problem loading properties file for I18n '%s'

#
# Precondition
#

objectIsNotInstanceOf = The '%s" object is not an instanceof '%s'
objectIsNotNull = The '%s' object is not null
stringsDoNotMatchExactly = The '%s' string has a value of '%s' and was expected to be '%s'
collectionIsEmpty = The '%s' collection is empty
mapIsEmpty = The '%s' map is empty
stringIsEmpty = The '%s' string is empty
objectIsNull = The '%s' object is null
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
package org.komodo.repository;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.komodo.common.util.CollectionUtil;
import org.komodo.common.util.HashCode;
import org.komodo.common.util.Precondition;
import org.komodo.common.util.StringUtil;
import org.komodo.repository.artifact.Artifact.Type;
import org.komodo.repository.deriver.DeriverUtil;
import org.overlord.sramp.client.SrampAtomApiClient;
import org.overlord.sramp.client.SrampClientQuery;
import org.overlord.sramp.client.query.ArtifactSummary;
import org.overlord.sramp.client.query.QueryResultSet;
import org.overlord.sramp.common.ArtifactType;
Expand All @@ -27,6 +32,7 @@
class AtomRepositoryManager implements RepositoryManager {

private static final Logger LOGGER = LoggerFactory.getLogger(AtomRepositoryManager.class);
private static final List<ArtifactType> NO_RESULTS = Collections.emptyList();

private final SrampAtomApiClient client;

Expand Down Expand Up @@ -151,6 +157,57 @@ public int hashCode() {
return HashCode.compute(this.url);
}

/**
* {@inheritDoc}
*
* @see org.komodo.repository.RepositoryManager#query(org.komodo.repository.RepositoryManager.QuerySettings)
*/
@Override
public List<ArtifactType> query(final QuerySettings settings) throws Exception {
final String query = DeriverUtil.buildQuery(settings);
LOGGER.debug("query built from settings '{}'", query); //$NON-NLS-1$

final SrampClientQuery clientQuery = this.client.buildQuery(query);

if (settings.ascending) {
clientQuery.ascending();
} else {
clientQuery.descending();
}

if (settings.count != -1) {
clientQuery.count(settings.count);
}

if (!StringUtil.isEmpty(settings.orderBy)) {
clientQuery.orderBy(settings.orderBy);
}

if (settings.startIndex != -1) {
clientQuery.startIndex(settings.startIndex);
}

if (!CollectionUtil.isEmpty(settings.resultColumns)) {
for (final String column : settings.resultColumns) {
clientQuery.propertyName(column);
}
}

final QueryResultSet results = clientQuery.query();

if (results.size() == 0) {
return NO_RESULTS;
}

final List<ArtifactType> artifacts = new ArrayList<ArtifactType>((int)results.size());

for (final ArtifactSummary summary : results) {
artifacts.add(summary.getType());
}

return artifacts;
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,12 @@
*/
package org.komodo.repository;

import org.komodo.common.util.StringUtil;

/**
* Constants used when working with the S-RAMP database and Teiid artifacts.
*/
public interface RepositoryConstants {

/**
* Common artifact relationship types.
*/
public enum RelationshipType {

/**
* A relationship between a data policy and its permissions.
*/
DATA_POLICY_PERMISSIONS("DataPolicyPermissions"), //$NON-NLS-1$

/**
* A relationship between a permission and its data policy.
*/
PERMISSION_DATA_POLICY("PermissionDataPolicy"), //$NON-NLS-1$

/**
* A relationship between a derived artifact and the document artifact. This is created by S-RAMP deriver framework.
*/
RELATED_DOCUMENT("relatedDocument"), //$NON-NLS-1$

/**
* A relationship between a schema/model and its sources.
*/
SCHEMA_SOURCES("SchemaSources"), //$NON-NLS-1$

/**
* A relationship between a source and its schema/model.
*/
SOURCE_SCHEMA("SourceSchema"); //$NON-NLS-1$

private final String name;

private RelationshipType(String name) {
this.name = (StringUtil.matches(name, "relatedDocument") ? name : (name + "Relationship")); //$NON-NLS-1$ //$NON-NLS-2$
}

/**
* @return the relationship type name (never <code>null</code> or empty)
*/
public String getName() {
return this.name;
}
}

/**
* Constants related to the S-RAMP database.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,59 @@
package org.komodo.repository;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import org.komodo.repository.artifact.Artifact;
import org.overlord.sramp.client.query.QueryResultSet;
import org.overlord.sramp.common.ArtifactType;
import org.s_ramp.xmlns._2010.s_ramp.BaseArtifactType;

/**
* Manages the interaction with the artifact repository.
*/
public interface RepositoryManager {

/**
* The settings to use for querying.
*/
public class QuerySettings {

/**
* The type of artifacts being queried for (can be <code>null</code>)
*/
public Artifact.Type artifactType;

/**
* Indicates if the results should be sorted in ascending order.
*/
public boolean ascending = true;

/**
* The max number of results to return.
*/
public int count = -1;

/**
* The property the results should be sorted by (can be <code>null</code> or empty).
*/
public String orderBy;

/**
* The property criteria of the artifacts to be returned in the results (can be <code>null</code> or empty).
*/
public Map<String, String> params;

/**
* The properties to return in the results (can be <code>null</code> or empty).
*/
public List<String> resultColumns;

/**
* The start index.
*/
public int startIndex = -1;
}

/**
* Adds a VDB to the repository. Caller should set additional artifact properties based on the VDB business object created
* from this artifact. Caller should ensure stream is closed. The file name is used to determine mime type.
Expand Down Expand Up @@ -48,13 +93,20 @@ BaseArtifactType addVdb(final InputStream content,
String getName();

/**
* @param newName the new name of the repository manager (can be <code>null</code> or empty)
* @return the repository URL (never <code>null</code> or empty)
*/
void setName(String newName);
String getUrl();

/**
* @return the repository URL (never <code>null</code> or empty)
* @param settings the query settings (cannot be <code>null</code>)
* @return the lightweight artifact query results (never <code>null</code>)
* @throws Exception if there is a problem running query
*/
String getUrl();
List<ArtifactType> query(final QuerySettings settings) throws Exception;

/**
* @param newName the new name of the repository manager (can be <code>null</code> or empty)
*/
void setName(final String newName);

}

0 comments on commit 1e8e252

Please sign in to comment.