Skip to content

Commit

Permalink
Merge pull request #236 from NASA-PDS/moar-lidvid-classes
Browse files Browse the repository at this point in the history
Add additional lidvid classes
fixes #234
  • Loading branch information
alexdunnjpl authored Jan 31, 2023
2 parents 94eae32 + 31b6919 commit 02b2ba9
Show file tree
Hide file tree
Showing 21 changed files with 286 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public interface RequestBuildContext
{
public boolean justLatest(); // return just the latest LIDVIDs
public boolean justLatest(); // return just the latest LIDVIDs. return false if request is made for a specific version
public List<String> getFields(); // must not return null but an empty list
public GroupConstraint getPresetCriteria(); // must not return null but an empty list
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package gov.nasa.pds.api.registry;

import gov.nasa.pds.api.registry.model.identifiers.PdsProductIdentifier;

import java.util.List;
import java.util.Map;

public interface RequestConstructionContext
{
public List<String> getKeywords(); // must not return null but an empty list
public Map<String, List<String>> getKeyValuePairs(); // must not return null but an empty map
public String getLIDVID(); // must not return null but an empty string
public PdsProductIdentifier getProductIdentifier();
public String getProductIdentifierString(); // must not return null but an empty string
public String getQueryString(); // must not return null but an empty string
public boolean isTerm(); // if true, then use QueryBuilders.termQuery otherwise use QueryBuilders.matchQuery
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@
import gov.nasa.pds.api.registry.exceptions.NothingFoundException;
import gov.nasa.pds.api.registry.exceptions.UnknownGroupNameException;
import gov.nasa.pds.api.registry.model.ErrorFactory;
import gov.nasa.pds.api.registry.model.LidVidUtils;
import gov.nasa.pds.api.registry.model.identifiers.LidVidUtils;

@Controller
public class SwaggerJavaTransmuter extends SwaggerJavaDeprecatedTransmuter implements ControlContext, BundlesApi, CollectionsApi, ClassesApi, ProductsApi
{
private static final Logger log = LoggerFactory.getLogger(SwaggerJavaTransmuter.class);
private static final Logger log = LoggerFactory.getLogger(SwaggerJavaTransmuter.class);
private final ObjectMapper objectMapper;
private final HttpServletRequest request;
private final HttpServletRequest request;

@Value("${server.contextPath}")
protected String contextPath;

@Autowired
protected HttpServletRequest context;

Expand All @@ -68,15 +68,15 @@ public URL getBaseURL()
if (this.proxyRunsOnDefaultPort())
{
baseURL = new URL(this.context.getScheme(), this.context.getServerName(), this.contextPath);
}
}
else
{
baseURL = new URL(this.context.getScheme(), this.context.getServerName(), this.context.getServerPort(), this.contextPath);
}

log.debug("baseUrl is " + baseURL.toString());
return baseURL;

}
catch (MalformedURLException e)
{
Expand All @@ -97,7 +97,7 @@ protected ResponseEntity<Object> processs (EndpointHandler handler, URIParameter
long begin = System.currentTimeMillis();
try
{
parameters.setAccept(this.request.getHeader("Accept")).setLidVid(this);
parameters.setAccept(this.request.getHeader("Accept")).setProductIdentifier(this);
if (parameters.getVerifyClassAndId()) LidVidUtils.verify (this, parameters);
return handler.transmute(this, parameters);
}
Expand All @@ -106,7 +106,7 @@ protected ResponseEntity<Object> processs (EndpointHandler handler, URIParameter
log.error("Application type not implemented", e);
return new ResponseEntity<Object>(ErrorFactory.build(e, this.request), HttpStatus.NOT_ACCEPTABLE);
}
catch (IOException e)
catch (IOException e)
{
log.error("Couldn't get or serialize response for content type " + this.request.getHeader("Accept"), e);
return new ResponseEntity<Object>(ErrorFactory.build(e, this.request), HttpStatus.INTERNAL_SERVER_ERROR);
Expand Down Expand Up @@ -142,12 +142,12 @@ protected ResponseEntity<Object> processs (EndpointHandler handler, URIParameter

private boolean proxyRunsOnDefaultPort()
{
return (("https".equals(this.context.getScheme()) && (this.context.getServerPort() == 443))
return (("https".equals(this.context.getScheme()) && (this.context.getServerPort() == 443))
|| ("http".equals(this.context.getScheme()) && (this.context.getServerPort() == 80)));
}

@Override
public ResponseEntity<Object> bundleList(@Valid List<String> fields, @Valid List<String> keywords,
public ResponseEntity<Object> bundleList(@Valid List<String> fields, @Valid List<String> keywords,
@Min(0) @Valid Integer limit, @Valid String q, @Valid List<String> sort,
@Min(0) @Valid Integer start) {
// TODO Auto-generated method stub
Expand Down Expand Up @@ -202,7 +202,7 @@ public ResponseEntity<Object> bundlesLidvidProducts(String identifier, @Valid Li
}

@Override
public ResponseEntity<Object> collectionList(@Valid List<String> fields, @Valid List<String> keywords,
public ResponseEntity<Object> collectionList(@Valid List<String> fields, @Valid List<String> keywords,
@Min(0) @Valid Integer limit, @Valid String q, @Valid List<String> sort,
@Min(0) @Valid Integer start) {
// TODO Auto-generated method stub
Expand Down Expand Up @@ -257,7 +257,7 @@ public ResponseEntity<Object> collectionsLidvidProductsLatest(String identifier,
}

@Override
public ResponseEntity<Object> productList(@Valid List<String> fields, @Valid List<String> keywords,
public ResponseEntity<Object> productList(@Valid List<String> fields, @Valid List<String> keywords,
@Min(0) @Valid Integer limit, @Valid String q, @Valid List<String> sort,
@Min(0) @Valid Integer start) {
// TODO Auto-generated method stub
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
import gov.nasa.pds.api.registry.ControlContext;
import gov.nasa.pds.api.registry.UserContext;
import gov.nasa.pds.api.registry.exceptions.LidVidNotFoundException;
import gov.nasa.pds.api.registry.model.LidVidUtils;
import gov.nasa.pds.api.registry.model.identifiers.LidVidUtils;
import gov.nasa.pds.api.registry.model.ProductVersionSelector;
import gov.nasa.pds.api.registry.model.identifiers.PdsProductIdentifier;
import gov.nasa.pds.api.registry.search.RequestBuildContextFactory;

/*
* Maybe not the most obvious properties class or bean or whatever name but
* here are some things that are being done and must be maintained
*
*
* 1. If the set value is null, then leave the default value in place.
* The reason for ignoring null, is that 100 different places do not have
* to test for null then do the default thing. The default thing when not
Expand All @@ -31,14 +32,14 @@
class URIParameters implements UserContext
{
private static final int SUMMARY_SAMPLE_SIZE = 100;

private boolean verifyClassAndId = false;
private String accept = "application/json";
private List<String> fields = new ArrayList<String>();
private String group = "";
private String identifier = "";
private List<String> keywords = new ArrayList<String>();
private String lidvid = "";
private PdsProductIdentifier productIdentifier = null;
private Integer limit = Integer.valueOf(0);
private boolean summaryOnly = true;
private String query = "";
Expand All @@ -60,7 +61,7 @@ class URIParameters implements UserContext
@Override
public Integer getLimit() { return limit; }
@Override
public String getLidVid() { return lidvid; }
public String getLidVid() { return productIdentifier != null ? productIdentifier.toString() : ""; }
@Override
public String getQuery() { return query; }
@Override
Expand Down Expand Up @@ -102,7 +103,7 @@ public URIParameters setKeywords(List<String> keywords)
}
public URIParameters setLimit(Integer limit)
{
/*
/*
* Note: Not too happy w/ having to put behavioral logic in a utility/container class, but
* there are just way too many places where this information is necessary and rather than
* duplicate it everywhere, this is the best place, for now, as it is the common object
Expand All @@ -119,9 +120,9 @@ public URIParameters setLimit(Integer limit)
}
return this;
}
public URIParameters setLidVid(ControlContext control) throws IOException, LidVidNotFoundException
public URIParameters setProductIdentifier(ControlContext control) throws IOException, LidVidNotFoundException
{
this.lidvid = LidVidUtils.resolve(this.getIdentifier(), ProductVersionSelector.TYPED,
this.productIdentifier = LidVidUtils.resolve(this.getIdentifier(), ProductVersionSelector.SPECIFIC,
control, RequestBuildContextFactory.empty());
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public enum ProductVersionSelector
*/
LATEST,
/**
* What the user typed but latest if a LID
* A specified version of a product
*/
TYPED
SPECIFIC
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import gov.nasa.pds.api.registry.exceptions.LidVidNotFoundException;
import gov.nasa.pds.api.registry.exceptions.MembershipException;
import gov.nasa.pds.api.registry.exceptions.UnknownGroupNameException;
import gov.nasa.pds.api.registry.model.identifiers.LidVidUtils;
import gov.nasa.pds.api.registry.model.identifiers.PdsProductIdentifier;
import gov.nasa.pds.api.registry.search.QuickSearch;
import gov.nasa.pds.api.registry.search.RequestBuildContextFactory;
import gov.nasa.pds.api.registry.util.GroupConstraintImpl;
Expand All @@ -29,10 +31,10 @@ private boolean isGrandchild (ReferencingLogicTransmuter idType)
private ReferencingLogicTransmuter resolveID (ControlContext context, UserContext input)
throws IOException, LidVidNotFoundException, UnknownGroupNameException
{
PdsProductIdentifier productIdentifier = LidVidUtils.resolve(input.getIdentifier(), ProductVersionSelector.SPECIFIC, context, RequestBuildContextFactory.empty());
return ReferencingLogicTransmuter.getByProductClass(
QuickSearch.getValue(context.getConnection(), input.getSelector() == ProductVersionSelector.LATEST,
LidVidUtils.resolve(input.getIdentifier(), ProductVersionSelector.TYPED, context, RequestBuildContextFactory.empty()),
"product_class"));
productIdentifier != null ? productIdentifier.toString() : "", "product_class"));
}

private RequestAndResponseContext search(ControlContext context, UserContext input, boolean isIdToGroup)
Expand All @@ -41,7 +43,7 @@ private RequestAndResponseContext search(ControlContext context, UserContext inp
// find all of the given group that reference the specified ID
ReferencingLogicTransmuter groupType = ReferencingLogicTransmuter.getBySwaggerGroup(input.getGroup());
ReferencingLogicTransmuter idType = this.resolveID(context, input);

if (idType == ReferencingLogicTransmuter.Bundle && groupType == ReferencingLogicTransmuter.Collection)
return RequestAndResponseContext.buildRequestAndResponseContext
(context, input, RefLogicBundle.children(context, input.getSelector(), input));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

import gov.nasa.pds.api.registry.model.identifiers.LidVidUtils;
import gov.nasa.pds.api.registry.model.identifiers.PdsLidVid;
import gov.nasa.pds.api.registry.model.identifiers.PdsProductIdentifier;
import org.opensearch.action.search.SearchRequest;
import org.opensearch.client.RequestOptions;
import org.opensearch.search.SearchHit;
Expand All @@ -33,9 +37,9 @@
import gov.nasa.pds.api.registry.util.GroupConstraintImpl;

/**
* Bundle Data Access Object (DAO).
* Bundle Data Access Object (DAO).
* Provides methods to get bundle information from opensearch.
*
*
* @author karpenko
*/
@Immutable
Expand All @@ -48,7 +52,7 @@ static Pagination<String> children (ControlContext control, ProductVersionSelect
{
log.info("Find children of a bundle");
return selection == ProductVersionSelector.ALL ?
getAllBundleCollectionLidVids(uid, control) :
getAllBundleCollectionLidVids(uid, control) :
getBundleCollectionLidVids(uid, control);
}

Expand All @@ -67,7 +71,7 @@ static private Pagination<String> getAllBundleCollectionLidVids(
// Get bundle by lidvid.
SearchRequest request = new SearchRequestFactory(RequestConstructionContextFactory.given(idContext.getLidVid()), ctlContext.getConnection())
.build(RequestBuildContextFactory.given(false, fields, ReferencingLogicTransmuter.Bundle.impl().constraints()), ctlContext.getConnection().getRegistryIndex());

// Call opensearch
SearchHit hit;
SearchHits hits = ctlContext.getConnection().getRestHighLevelClient().search(request, RequestOptions.DEFAULT).getHits();
Expand All @@ -84,36 +88,37 @@ static private Pagination<String> getAllBundleCollectionLidVids(

ids.addAll(lidvids.convert(fieldMap.get("ref_lid_collection")));
ids.addAll(lidvids.convert(fieldMap.get("ref_lid_collection_secondary")));
ids = LidVidUtils.getAllLidVidsByLids(ctlContext,
ids = LidVidUtils.getAllLidVidsByLids(ctlContext,
RequestBuildContextFactory.given(false, "lidvid", ReferencingLogicTransmuter.Collection.impl().constraints()),
ids);
lidvids.addAll(ids);
return lidvids;
}

/**
* Get collections of a bundle by bundle LIDVID.
* If a bundle has LIDVID collection references, then those collections are returned.
* Get collections of a bundle by bundle LIDVID.
* If a bundle has LIDVID collection references, then those collections are returned.
* If a bundle has LID collection references, then the latest versions of collections are returned.
* @return a list of collection LIDVIDs
* @throws IOException IO exception
* @throws LidVidNotFoundException LIDVID not found exception
*/
static private Pagination<String> getBundleCollectionLidVids(
LidvidsContext idContext,
ControlContext ctlContext)
ControlContext ctlContext)
throws IOException, LidVidNotFoundException
{
// TODO: Fully convert this function's internals (and eventually, interface) to use PdsProductIdentifier classes instead of strings
// Fetch collection references only.
List<String> fields = Arrays.asList("ref_lidvid_collection","ref_lidvid_collection_secondary",
"ref_lid_collection", "ref_lid_collection_secondary");

// Get bundle by lidvid.
SearchRequest request = new SearchRequestFactory(RequestConstructionContextFactory.given(idContext.getLidVid()), ctlContext.getConnection())
.build(RequestBuildContextFactory.given(true, fields,
ReferencingLogicTransmuter.Bundle.impl().constraints()),
ctlContext.getConnection().getRegistryIndex());

// Call opensearch
SearchHit hit;
SearchHits hits = ctlContext.getConnection().getRestHighLevelClient().search(request, RequestOptions.DEFAULT).getHits();
Expand All @@ -126,11 +131,11 @@ static private Pagination<String> getBundleCollectionLidVids(
List<String> ids = new ArrayList<String>();
Map<String, Object> fieldMap = hit.getSourceAsMap();
PaginationLidvidBuilder lidvids = new PaginationLidvidBuilder(idContext);

ids.addAll(lidvids.convert(fieldMap.get("ref_lidvid_collection")));
ids.addAll(lidvids.convert(fieldMap.get("ref_lidvid_collection_secondary")));
// !!! NOTE !!!

// !!! NOTE !!!
// Harvest converts LIDVID references to LID references and stores them in
// "ref_lid_collection" and "ref_lid_collection_secondary" fields.
// To get "real" LID references, we have to exclude LIDVID references from these fields.
Expand All @@ -144,20 +149,24 @@ static private Pagination<String> getBundleCollectionLidVids(
lidsToRemove.add(lid);
}
}

// Lid references (e.g., Kaguya bundle) plus LIDVID references converted by Harvest
List<String> lids = new ArrayList<String>();
lids.addAll(lidvids.convert(fieldMap.get("ref_lid_collection")));
lids.addAll(lidvids.convert(fieldMap.get("ref_lid_collection_secondary")));
List<String> lidStrings = new ArrayList<String>();
lidStrings.addAll(lidvids.convert(fieldMap.get("ref_lid_collection")));
lidStrings.addAll(lidvids.convert(fieldMap.get("ref_lid_collection_secondary")));

// Get "real" LIDs
if(!lidsToRemove.isEmpty())
{ lids.removeAll(lidsToRemove); }
{ lidStrings.removeAll(lidsToRemove); }

// Get the latest versions of LIDs
lidvids.addAll(LidVidUtils.getLatestLidVidsByLids(ctlContext,
RequestBuildContextFactory.given(true, "lid",
ReferencingLogicTransmuter.Collection.impl().constraints()), lids));
List<PdsProductIdentifier> productIdentifiers = lidStrings.stream().map(PdsProductIdentifier::fromString).collect(Collectors.toList());
List<PdsLidVid> latestLidVids = LidVidUtils.getLatestLidVidsForProductIdentifiers(
ctlContext,
RequestBuildContextFactory.given(true, "lid", ReferencingLogicTransmuter.Collection.impl().constraints()),
productIdentifiers);
List<String> latestLidVidStrings = latestLidVids.stream().map(PdsLidVid::toString).collect(Collectors.toList());
lidvids.addAll(latestLidVidStrings);
return lidvids;
}

Expand Down Expand Up @@ -191,7 +200,7 @@ public RequestAndResponseContext member(ControlContext context, UserContext inpu
(context, input, RefLogicBundle.children(context, input.getSelector(), input));
}


@Override
public RequestAndResponseContext memberOf(ControlContext context, UserContext input, boolean twoSteps)
throws ApplicationTypeException, IOException, LidVidNotFoundException, MembershipException,
Expand Down
Loading

0 comments on commit 02b2ba9

Please sign in to comment.