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

JNG-5368 add access instance helper methods #49

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -2,9 +2,9 @@

import type { AxiosResponse } from 'axios';
import type { JudoDownloadFile, JudoMetaData, JudoToken } from '../data-api/common';
import type { AccessService } from '../data-service';
import { JudoAxiosService } from './JudoAxiosService';
{{# if application.principal }}import type { {{ classDataName application.principal "Stored" }} } from '../data-api';{{/ if }}
import { AccessService } from '../data-service';
import { {{ accessJoinedImportTokens application }} } from '../data-api';

export class AccessServiceImpl extends JudoAxiosService implements AccessService {
{{# if application.principal }}
Expand Down Expand Up @@ -69,4 +69,28 @@ export class AccessServiceImpl extends JudoAxiosService implements AccessService
});
return response;
}
{{# each (getAccessRelationsTypes application) as |relation| }}
{{# if relation.isListable }}
{{# if relation.isCollection }}
/**
* @return {Promise<{{ classDataName relation.target "Stored" }} | undefined>}
*/
async findInstanceOf{{ firstToUpper relation.name }}(identifier: string): Promise<{{ classDataName relation.target "Stored" }} | undefined> {
try {
const path = '{{ restPath (getRelationOwnerAsClassType relation) "/" relation.name "/~list" }}';
const response = await this.axios.post(this.getPathForActor(path), {
_identifier: identifier,
});

if (Array.isArray(response.data) && response.data.length === 1) {
return response.data[0];
}
return undefined;
} catch(error) {
return undefined;
}
}
{{/ if }}
{{/ if }}
{{/ each }}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,30 @@
@TemplateHelper
public class UiServiceHelper extends StaticMethodValueResolver {

public static Collection<RelationType> getNotAccessRelationsTypes(Application application) {
public static List<RelationType> getNotAccessRelationsTypes(Application application) {
return (List<RelationType>) application.getRelationTypes().stream()
.filter(r -> !((RelationType) r).isIsAccess())
.sorted(Comparator.comparing(NamedElement::getFQName))
.collect(Collectors.toList());
.toList();
}

public static Collection<RelationType> getAccessRelationsTypes(Application application) {
public static List<RelationType> getAccessRelationsTypes(Application application) {
return (List<RelationType>) application.getRelationTypes().stream()
.filter(r -> ((RelationType) r).isIsAccess())
.sorted(Comparator.comparing(NamedElement::getFQName))
.collect(Collectors.toList());
.toList();
}

public static String accessJoinedImportTokens(Application application) {
HashSet<String> tokens = new HashSet<>();
if (application.getPrincipal() != null) {
tokens.add(classDataName(application.getPrincipal(), "Stored"));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stream pricipalToken = Stream.empty();
if (application.getPrincipal() != null) {
   principalToken = Stream.of(classDataName(application.getPrincipal(), "Stored"));
}
return Stream.concat(
              getAccessRelationsTypes(application)
                .stream(), 
                .map(r -> classDataName(relation.getTarget(), "Stored")), 
              principalToken)
         .distinct()
         .sorted()
         .collect(Collectors.joining(","));

List<RelationType> relations = getAccessRelationsTypes(application);
relations.forEach(relation -> {
tokens.add(classDataName(relation.getTarget(), "Stored"));
});
return String.join(", ", tokens);
}

public static String joinedTokensForApiImport(RelationType relation){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
{{> fragment.header.hbs }}

import type { JudoDownloadFile, JudoMetaData } from '../data-api/common';
{{# if application.principal }}import { {{ classDataName application.principal "Stored" }} } from '../data-api';{{/ if }}
import { {{ accessJoinedImportTokens application }} } from '../data-api';

export interface AccessService {
{{# if application.principal }}
{{# if application.principal }}
getPrincipal(): Promise<{{ classDataName application.principal "Stored" }}>;
{{/if}}
{{/ if }}

getMetaData(): Promise<JudoMetaData>;

uploadFile(attributePath: string, file: File): Promise<string>;

downloadFile(downloadToken: string, disposition: 'inline' | 'attachment'): Promise<any>;

{{# each (getAccessRelationsTypes application) as |relation| }}
{{# if relation.isListable }}
{{# if relation.isCollection }}
findInstanceOf{{ firstToUpper relation.name }}(identifier: string): Promise<{{ classDataName relation.target "Stored" }} | undefined>;
{{/ if }}
{{/ if }}
{{/ each }}
}
Loading