dimanche 6 octobre 2019

how to implement a functional interface with different first parameter in each sub-enum

It's really hard to summarize the problem but I will try my best and hope you get it, I have a enum that implements a functional interface (Execution) where the only method in Execution is execute that accept two parameters

1: AbstractRepository: spring-jpa interface

2: String

the thing is with each sub enum I need to implement execute method but rather than making it accept AbstractRepository I want to make it accept interface that extends AbstractRepository.

this is the enum with the functional interface.

package com.deepTech.Familytree.enums;

import com.deepTech.Familytree.domain.ImageBox;
import com.deepTech.Familytree.domain.Person;
import com.deepTech.Familytree.exception.PersonException;
import com.deepTech.Familytree.repository.AbstractRepository;

import java.util.Optional;

import static com.deepTech.Familytree.config.AuthDataConfig.getUser;
import static com.deepTech.Familytree.exception.PersonException.ExceptionType.PERSON_NOT_FOUND;

// fix the name later!!!
public enum SsssEnum implements Execution {


    UPLOAD_PERSON_FILE() {
        @Override
               // rather than AbstractRepository I want to make accept repository that extends AbstractRepository
        public void execute(AbstractRepository repository, String filename) {
            Optional<Person> person1 = (Optional<Person>) repository
                    .finByTEm(getUser().getEmail());

            person1.map(person -> person.copyByImage(new ImageBox(filename)));


            repository.save(person1.orElseThrow(() -> new PersonException(PERSON_NOT_FOUND)));
        }
    },

    DELETE_PERSON_FILE() {
        @Override
        public void execute(AbstractRepository repository, String filename) {
            Optional<Person> person1 = (Optional<Person>) repository
                    .finByTEm(getUser().getEmail());

            repository.save(person1
                    .map(Person::withoutImage)
                    .orElseThrow(() -> new PersonException(PERSON_NOT_FOUND)));

        }
    },
    UPLOAD_VIP_PERSON_FILE() {
        @Override
        public void execute(AbstractRepository repository, String filename) {

        }
    },

    DELETE_VIP_PERSON_FILE() {
        @Override
        public void execute(AbstractRepository repository, String filename) {


        }
    },
    UPLOAD_NEWS_FILE() {
        @Override
        public void execute(AbstractRepository repository, String filename) {

        }
    },

    DELETE_NEWS_FILE() {
        @Override
        public void execute(AbstractRepository repository, String filename) {


        }
    };

}

@FunctionalInterface
interface Execution {
    void execute(AbstractRepository repository, String filename);
}

@Service
@RequiredArgsConstructor
public class PersonServiceImpl {
...
 /* somecode */
...
    public UploadFileResponse uploadFile(MultipartFile file) {
        String fileName = fileStorageService.storeFile(file);

        String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
                .path("/downloadFile/")
                .path(fileName)
                .toUriString();

        fileManagementService.uploadFile(file, UPLOAD_PERSON_FILE, personRepository);

        return new UploadFileResponse(fileName, fileDownloadUri,
                file.getContentType(), file.getSize());
    }
...
 /* somecode */
...
}

and here FileManagementService class

@Service
@RequiredArgsConstructor
public class FileManagementService {
...
 /* somecode */
...
    public UploadFileResponse uploadFile(MultipartFile file, SsssEnum ssssEnum, AbstractRepository neo4jRepository) {
        String fileName = fileStorageService.storeFile(file);

        String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
                .path("/downloadFile/")
                .path(fileName)
                .toUriString();

        ssssEnum.execute(neo4jRepository, fileName);

        return new UploadFileResponse(fileName, fileDownloadUri,
                file.getContentType(), file.getSize());
    }
...
 /* somecode */
...
}

can anybody help please?

Aucun commentaire:

Enregistrer un commentaire