GAIA-X4PLC-AAD / portal

1 stars 2 forks source link

Rework search Page Resources: Filter based on values from shape 2 #101

Closed devbysp closed 2 months ago

devbysp commented 2 months ago

Tasks:

Required

/**
 * Returns all resource formats. Resource formats are listed in a node of a resource called Format. The possible
 * values are contained inside the http://www.w3.org/ns/shacl#in property.
 *
 * @param ontologies serving as the source of information
 * @return a list of resource formats
 */
export const getResourceFormats = (ontologies: Ontology[]): string[] => {
  const dataResourceOntologies = ontologies
    .filter(ontology => ontology.relatedShapes
      .some(relatedShape => isSubclassOfDataResource(ontology, relatedShape))
    )
  const formatShapes = dataResourceOntologies
    .map(ontology => ontology.relatedShapes
      .filter(relatedShape => relatedShape.targetClasses
        .some(targetClass => targetClass.endsWith('/Format')))
    )
    .flat()
  const formatShapeProperties = formatShapes
    .map(shape => shape.properties
      .filter(property => property.propertyId.endsWith('/formatType'))
    )
    .flat()
  const inPropertyParameters = formatShapeProperties
    .map(property => property.propertyValues
      .filter(propertyParameter => propertyParameter.type === 'http://www.w3.org/ns/shacl#in')
    )
    .flat()
  return Array.from(new Set<string>(
    inPropertyParameters.map(parameter => parameter.value).flat()
  ));
}

Other thing solved

  /**
   * Returns all resources of type included in the type asset list passed in as parameter.
   *
   * @param authContext authorization token for server calls
   * @param typeFilters the list of requested resource types
   */
  async getAllResources(authContext: AuthContextType, typeFilters: Asset[]): Promise<ResourceInput> {
    const whereDataResource = typeFilters.length ? `
    WHERE ANY (label IN labels(n) WHERE label IN [ '${typeFilters.map(asset => asset.label).join('\', \'')}'])
      AND 'DataResource' IN labels(n)
      AND NOT n.uri STARTS WITH 'bnode://'`
      : ''

    const matchFormatNode = `
    OPTIONAL MATCH(m)
    WHERE n.uri IN m.claimsGraphUri 
      AND ANY(label IN labels(m) WHERE label CONTAINS 'Format')`

    return cypherQuery(authContext, {
      statement: `
      MATCH (n) 
      ${whereDataResource} 
      ${matchFormatNode}
      RETURN 
        properties(n) AS properties, 
        labels(n) AS labels, 
        properties(m).type AS format`,
    })
  }