Last active
November 25, 2025 11:52
-
-
Save DattatreyaReddy/250e7a1f8de77026f7527e5ef16dfeae to your computer and use it in GitHub Desktop.
DocumentGenerator Annotation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Target(ElementType.TYPE) | |
| @Retention(RetentionPolicy.RUNTIME) | |
| @Service | |
| public @interface DocumentGenerator { | |
| DocumentGenerationEntityType value(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.assist.core.factory; | |
| import com.assist.commons.annotations.DocumentGenerator; | |
| import com.assist.commons.documentGeneration.DocumentInfoDto; | |
| import com.assist.commons.documentGeneration.constants.DocumentGenerationEntityType; | |
| import com.assist.commons.documentGeneration.service.EntityDocumentGenerationService; | |
| import java.util.HashMap; | |
| import java.util.Map; | |
| import java.util.Objects; | |
| import org.apache.commons.lang.NotImplementedException; | |
| import org.springframework.beans.BeansException; | |
| import org.springframework.beans.factory.InitializingBean; | |
| import org.springframework.beans.factory.ListableBeanFactory; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.context.ApplicationContext; | |
| import org.springframework.context.ApplicationContextAware; | |
| import org.springframework.stereotype.Service; | |
| @Service | |
| public class DocumentGeneratorBeanFactory implements ApplicationContextAware, InitializingBean { | |
| private final Map<DocumentGenerationEntityType, EntityDocumentGenerationService> typeBeanMap = | |
| new HashMap<>(); | |
| @Autowired | |
| private ListableBeanFactory listableBeanFactory; | |
| private ApplicationContext applicationContext; | |
| public <EntityInfoDto extends DocumentInfoDto, EntityDto> EntityDocumentGenerationService<EntityInfoDto, EntityDto> getBean( | |
| DocumentGenerationEntityType entityType) { | |
| return typeBeanMap.get(entityType); | |
| } | |
| @Override | |
| public void afterPropertiesSet() throws Exception { | |
| initializeTypeBeanMap(); | |
| } | |
| @Override | |
| public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { | |
| this.applicationContext = applicationContext; | |
| } | |
| //Method to initialize or create a map to store entityType and corresponding bean of entityType | |
| private void initializeTypeBeanMap() { | |
| Map<String, Object> beansWithAnnotation = | |
| applicationContext.getBeansWithAnnotation(DocumentGenerator.class); | |
| for (String beanName : beansWithAnnotation.keySet()) { | |
| EntityDocumentGenerationService documentGenerationService = | |
| listableBeanFactory.getBean(beanName, EntityDocumentGenerationService.class); | |
| DocumentGenerator beanHandler = | |
| listableBeanFactory.findAnnotationOnBean(beanName, DocumentGenerator.class); | |
| if (Objects.isNull(beanHandler)) { | |
| throw new NotImplementedException(beanName + " Bean not found!"); | |
| } | |
| DocumentGenerationEntityType entityType = beanHandler.value(); | |
| typeBeanMap.put(entityType, documentGenerationService); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// factory usage | |
| private <EntityInfo extends DocumentInfoDto, EntityDto> | |
| EntityDocumentGenerationService<EntityInfo, EntityDto> getEntityDocumentGenerationService( | |
| DocumentGenerationEntityType entityType) { | |
| try { | |
| return documentGeneratorBeanFactory.getBean(entityType); | |
| } catch (Exception e) { | |
| throw new IllegalStateException(entityType + " PDF Implementation Not Found"); | |
| } | |
| } | |
| private <EntityInfo extends DocumentInfoDto, EntityDto> | |
| EntityDocumentCreationService<EntityInfo, EntityDto> getEntityDocumentCreationService( | |
| DocumentGenerationEntityType entityType) { | |
| EntityDocumentGenerationService<EntityInfo, EntityDto> generationService = | |
| this.<EntityInfo, EntityDto>getEntityDocumentGenerationService(entityType); | |
| if (!(generationService instanceof EntityDocumentCreationService)) { | |
| throw new IllegalStateException( | |
| "Can not create document for " + entityType.getDisplayName()); | |
| } | |
| return (EntityDocumentCreationService<EntityInfo, EntityDto>) generationService; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment