package uk.ac.ebi.subs.api.resourceAssembly;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.ResourceProcessor;
import org.springframework.stereotype.Component;
import uk.ac.ebi.subs.api.DomainController;
import uk.ac.ebi.subs.api.SubmissionContentsController;
import uk.ac.ebi.subs.data.Submission;
import uk.ac.ebi.subs.data.SubmissionLinks;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;


@Component
public class SubmissionResourceProcessor implements ResourceProcessor<Resource<Submission>> {

    private Class<SubmissionContentsController> submittablesControllerClass = SubmissionContentsController.class;

    private Pageable defaultPageRequest() {
        return new PageRequest(0, 1);
    }


    @Override
    public Resource<Submission> process(Resource<Submission> resource) {

        if (resource.getContent().getDomain() != null && resource.getContent().getDomain().getName() != null) {
            resource.add(
                    linkTo(
                            methodOn(DomainController.class)
                                    .getDomain(resource.getContent().getDomain().getName())
                    ).withRel("domain")
            );
        }

        resource.add(
                linkTo(
                        methodOn(submittablesControllerClass)
                                .submissionAnalyses(
                                        resource.getContent().getId(),
                                        defaultPageRequest()
                                ))
                        .withRel(SubmissionLinks.ANALYSIS)
        );

        resource.add(
                linkTo(
                        methodOn(submittablesControllerClass)
                                .submissionAssays(
                                        resource.getContent().getId(),
                                        defaultPageRequest()
                                ))
                        .withRel(SubmissionLinks.ASSAY)
        );

        resource.add(
                linkTo(
                        methodOn(submittablesControllerClass)
                                .submissionAssayData(
                                        resource.getContent().getId(),
                                        defaultPageRequest()
                                ))
                        .withRel(SubmissionLinks.ASSAY_DATA)
        );

        resource.add(
                linkTo(
                        methodOn(submittablesControllerClass)
                                .submissionEgaDacs(
                                        resource.getContent().getId(),
                                        defaultPageRequest()
                                ))
                        .withRel(SubmissionLinks.EGA_DAC)
        );

        resource.add(
                linkTo(
                        methodOn(submittablesControllerClass)
                                .submissionEgaDacPolicies(
                                        resource.getContent().getId(),
                                        defaultPageRequest()
                                ))
                        .withRel(SubmissionLinks.EGA_DAC_POLICY)
        );

        resource.add(
                linkTo(
                        methodOn(submittablesControllerClass)
                                .submissionEgaDatasets(
                                        resource.getContent().getId(),
                                        defaultPageRequest()
                                ))
                        .withRel(SubmissionLinks.EGA_DATASET)
        );

        resource.add(
                linkTo(
                        methodOn(submittablesControllerClass)
                                .submissionProjects(
                                        resource.getContent().getId(),
                                        defaultPageRequest()
                                ))
                        .withRel(SubmissionLinks.PROJECT)
        );

        resource.add(
                linkTo(
                        methodOn(submittablesControllerClass)
                                .submissionProtocols(
                                        resource.getContent().getId(),
                                        defaultPageRequest()
                                ))
                        .withRel(SubmissionLinks.PROTOCOL)
        );

        resource.add(
                linkTo(
                        methodOn(submittablesControllerClass)
                                .submissionSamples(
                                        resource.getContent().getId(),
                                        defaultPageRequest()
                                ))
                        .withRel(SubmissionLinks.SAMPLE)
        );

        resource.add(
                linkTo(
                        methodOn(submittablesControllerClass)
                                .submissionSampleGroups(
                                        resource.getContent().getId(),
                                        defaultPageRequest()
                                ))
                        .withRel(SubmissionLinks.SAMPLE_GROUP)
        );

        resource.add(
                linkTo(
                        methodOn(submittablesControllerClass)
                                .submissionStudies(
                                        resource.getContent().getId(),
                                        defaultPageRequest()
                                ))
                        .withRel(SubmissionLinks.STUDY)
        );

        return resource;
    }


}

@@ -39,16 +39,79 @@
 embly;%0A%0A
+import org.springframework.beans.factory.annotation.Autowired;%0A
 import o
@@ -203,16 +203,91 @@
 geable;%0A
+import org.springframework.data.rest.webmvc.support.RepositoryEntityLinks;%0A
 import o
@@ -608,16 +608,140 @@
 onLinks;
+%0Aimport uk.ac.ebi.subs.repository.model.SubmissionStatus;%0Aimport uk.ac.ebi.subs.repository.repos.SubmissionStatusRepository;
 %0A%0Aimport
@@ -1204,481 +1204,441 @@
  %7D%0A%0A
-%0A    @Override%0A    public Resource%3CSubmission%3E process(Resource%3CSubmission%3E resource) %7B%0A%0A        if (resource.getContent().getDomain() != null && resource.getContent().getDomain().getName() != null) %7B%0A            resource.add(%0A                    linkTo(%0A                            methodOn(DomainController.class)%0A                                    .getDomain(resource.getContent().getDomain().getName())
+    @Autowired private SubmissionStatusRepository submissionStatusRepository;%0A%0A    @Autowired private RepositoryEntityLinks repositoryEntityLinks;%0A%0A    @Override%0A    public Resource%3CSubmission%3E process(Resource%3CSubmission%3E resource) %7B%0A%0A        addStatusRel(resource);%0A        addDomainRel(resource);%0A        addContentsRels(resource);%0A%0A        return resource;
 %0A    
+%7D%0A%0A
     
-            ).withRel(%22domain%22)%0A            );%0A        %7D%0A
+private void addContentsRels(Resource%3CSubmission%3E resource) %7B
 %0A   
@@ -6041,32 +6041,732 @@
  );%0A
-%0A        return resource
+    %7D%0A%0A    private void addDomainRel(Resource%3CSubmission%3E resource) %7B%0A        if (resource.getContent().getDomain() != null && resource.getContent().getDomain().getName() != null) %7B%0A            resource.add(%0A                    linkTo(%0A                            methodOn(DomainController.class)%0A                                    .getDomain(resource.getContent().getDomain().getName())%0A                    ).withRel(%22domain%22)%0A            );%0A        %7D%0A    %7D%0A%0A    private void addStatusRel(Resource%3CSubmission%3E resource) %7B%0A        SubmissionStatus status = submissionStatusRepository.findBySubmission(resource.getContent());%0A        resource.add(%0A                repositoryEntityLinks.linkToSingleResource(status)%0A        )
 ;%0A  

add status res