Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional lidvid classes #236

Merged
merged 15 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Down Expand Up @@ -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,14 @@
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.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,7 +31,7 @@
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>();
Expand Down Expand Up @@ -102,7 +102,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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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.search.QuickSearch;
import gov.nasa.pds.api.registry.search.RequestBuildContextFactory;
import gov.nasa.pds.api.registry.util.GroupConstraintImpl;
Expand Down Expand Up @@ -41,7 +42,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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import gov.nasa.pds.api.registry.model.identifiers.LidVidUtils;
import gov.nasa.pds.api.registry.model.identifiers.PdsLid;
import gov.nasa.pds.api.registry.model.identifiers.PdsLidVid;
import gov.nasa.pds.api.registry.model.identifiers.PdsProductIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -81,32 +86,39 @@ private static Pagination<String> childrenLatest (ControlContext control, Lidvid
static Pagination<String> parents (ControlContext control, ProductVersionSelector selection, LidvidsContext uid)
throws IOException, LidVidNotFoundException
{
// TODO: Fully convert this function's internals (and eventually, interface) to use PdsProductIdentifier classes instead of strings
List<String> keys = Arrays.asList("ref_lid_collection", "ref_lid_collection_secondary",
"ref_lidvid_collection", "ref_lidvid_collection_secondary");
List<String> sortedLids;
List<String> sortedLidStrings;
Set<String> lids = new HashSet<String>();
String lid = LidVidUtils.parseLid(uid.getLidVid());
PdsProductIdentifier productIdentifier = PdsProductIdentifier.fromString(uid.getLidVid());
PaginationLidvidBuilder bundleLidvids = new PaginationLidvidBuilder(uid);

log.info("Find parents of a colletion -- both all and latest");
log.info("Find parents of collenction: " + uid.getLidVid() + " --- " + LidVidUtils.parseLid(uid.getLidVid()));
log.info("Find parents of a collection -- both all and latest");
log.info("Find parents of collection: " + uid.getLidVid() + " --- " + productIdentifier.getLid().toString());
for (String key : keys)
{
for (final Map<String,Object> kvp : new HitIterator(control.getConnection().getRestHighLevelClient(),
new SearchRequestFactory(RequestConstructionContextFactory.given(key, lid, true), control.getConnection())
new SearchRequestFactory(RequestConstructionContextFactory.given(key, productIdentifier.getLid().toString(), true), control.getConnection())
.build(RequestBuildContextFactory.given(true, "lid", ReferencingLogicTransmuter.Bundle.impl().constraints()),
control.getConnection().getRegistryIndex())))
{
lids.addAll(bundleLidvids.convert(kvp.get("lid")));
}
}
sortedLids = new ArrayList<String>(lids);
Collections.sort(sortedLids);
sortedLidStrings = new ArrayList<String>(lids);
Collections.sort(sortedLidStrings); // TODO: Implement comparison for PdsLids (only with other PdsLids)
List<PdsProductIdentifier> sortedLids = sortedLidStrings.stream().map(PdsLid::fromString).collect(Collectors.toList());

if (selection == ProductVersionSelector.ALL){
bundleLidvids.addAll(LidVidUtils.getAllLidVidsByLids(control, RequestBuildContextFactory.empty(), sortedLidStrings));
}
else {
List<PdsLidVid> latestLidvids = LidVidUtils.getLatestLidVidsForProductIdentifiers(control, RequestBuildContextFactory.empty(), sortedLids);
List<String> latestLidVidStrings = latestLidvids.stream().map(PdsLidVid::toString).collect(Collectors.toList());
bundleLidvids.addAll(latestLidVidStrings);
}

if (selection == ProductVersionSelector.ALL)
bundleLidvids.addAll (LidVidUtils.getAllLidVidsByLids(control, RequestBuildContextFactory.empty(), sortedLids));
else
bundleLidvids.addAll (LidVidUtils.getLatestLidVidsByLids(control, RequestBuildContextFactory.empty(), sortedLids));

return bundleLidvids;
}
Expand Down
Loading