/** * Updates a BaseEntity in the database * * @param value data to edit * @param fieldType type of baseentity * @return edited base entity * @throws JSONException the exception */ private BaseEntity parseBaseEntity(final JSONObject value, final Class fieldType) throws JSONException { final JpaRepository repository = getFieldRepository(fieldType); Optional<BaseEntity> entity = Optional.empty(); if (value.has("id") && (!value.isNull("id"))) { final String id = value.get("id").toString(); entity = Optional.ofNullable((BaseEntity) repository.findOne(id)); } if (!entity.isPresent()) { try { entity = Optional.ofNullable((BaseEntity) Class.forName(fieldType.getName()).newInstance()); } catch (InstantiationException | ClassNotFoundException | IllegalAccessException e1) { logger.info("[FormParse] [parseBaseEntity] Failure To Create Class Instance", e1); } } entity.ifPresent(e -> { final ParameterMapParser parser = new ParameterMapParser(value); parser.loopData((key, data) -> writeToEntity(e, key, data)); repository.saveAndFlush(e); }); return entity.orElseGet(null); }
/** * Creates a simple mock for a {@link JpaRepository} that mocks the * {@link JpaRepository#getOne(java.io.Serializable)} to return a base * entity that only has its id set. * * @param <U> type of repository * @param <T> type of repository entity * @param repositoryClass class of repository to be created * @param entityClass class of the entity to be created and returned by that mock * @param id the id that will be set on the created entity * @return */ static public <U extends JpaRepository<T, Long>, T extends BaseEntity> U getJpaRepositoryMockForGetOne(Class<U> repositoryClass, Class<T> entityClass, Long id) { try { T baseEntity = entityClass.newInstance(); baseEntity.setId(id); U mock = mock(repositoryClass); when(mock.getOne(id)).thenReturn(baseEntity); return mock; } catch (IllegalAccessException | InstantiationException e) { throw new RuntimeException("Can't create mock for repository", e); } }
private Map<Class<?>, Set<Class<?>>> scanForJpaRepositories(final String basePackage) { final Map<Class<?>, Set<Class<?>>> jpaRepositories = new HashMap<Class<?>, Set<Class<?>>>(); final ClassPathScanner scanner = new ClassPathScanner().withInterfacesOnly(); scanner.addIncludeFilter(new AssignableTypeFilter(JpaRepository.class)); final Set<BeanDefinition> candidateComponents = scanner.findCandidateComponents(basePackage); for (final BeanDefinition bd : candidateComponents) { final String beanClassName = bd.getBeanClassName(); final Class<?> repositoryClass = Reflections.classForName(beanClassName); if (!IDao.class.isAssignableFrom(repositoryClass)) { final Class<?>[] typeArguments = Reflections.resolveTypeArguments(repositoryClass, JpaRepository.class); Assertions.assertThat(typeArguments.length).isEqualTo(2); final Class<?> entity = typeArguments[0]; Set<Class<?>> set = jpaRepositories.get(entity); if (set == null) { set = new HashSet<Class<?>>(); jpaRepositories.put(entity, set); } Assertions.assertThat(set.add(repositoryClass)).isTrue(); } } return jpaRepositories; }
/** * Parses a comma separated value of database indexes. * Retrieves all the items from the database and adds them to an array to save them to the object * * @param value CSV of indexes * @param repositoryClass repository of entities * @return array of entities */ private Collection<BaseEntity> parseCSVDatabaseIndexes(final String value, final Class repositoryClass) { final Set<BaseEntity> elements = new HashSet<>(); final JpaRepository repository = getFieldRepository(repositoryClass); Arrays.stream(value.split(",")) .distinct() .filter(Objects::nonNull) .filter(UUIDConverter::isUUID) .forEach(id -> elements.add((BaseEntity) repository.getOne(id))); return elements; }
public static <ENTITY, REPO extends JpaRepository<ENTITY, ?> & JpaSpecificationExecutor> SpecificationBuilder<ENTITY> selectFrom(REPO repository) { SpecificationBuilder<ENTITY> builder = new SpecificationBuilder<>(); builder.repository = repository; builder.specification = new SpecificationImpl(); return builder; }
@BeforeMethod @SuppressWarnings({"unchecked"}) public void beforeMethod() { repository = mock(JpaRepository.class); presenter = new TestPresenter(repository, new EntityUtil()); ApplicationContext applicationContext = mock(ApplicationContext.class); testView = mock(TestView.class); when(applicationContext.getBean(TestView.class)).thenReturn(testView); presenter.setApplicationContext(applicationContext); }
@Override protected <T, ID extends Serializable> JpaRepository<?, ?> getTargetRepository( RepositoryMetadata metadata, EntityManager entityManager) { JpaEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata .getDomainType()); return new GenericRepositoryImpl(entityInformation, entityManager); // custom // implementation }
@After public void clearRepositories() throws Exception { repositories.forEach(JpaRepository::deleteAllInBatch); }
public void setDao(final JpaRepository<T, String> repository) { this.repository = repository; }
public static <ENTITY, REPO extends JpaRepository<ENTITY, ?> & JpaSpecificationExecutor> SpecificationBuilder<ENTITY> selectDistinctFrom(REPO repository) { SpecificationBuilder<ENTITY> builder = selectFrom(repository); builder.distinct(); return builder; }
public MagicService(JpaRepository<T, String> jpaRepository) { this.jpaRepository = jpaRepository; }
@Override protected JpaRepository<HuntingClubGroup, Long> getRepository() { return huntingClubGroupRepository; }
@Override protected JpaRepository<HuntingClub, Long> getRepository() { return huntingClubRepository; }
@Override protected JpaRepository<HuntingClubArea, Long> getRepository() { return huntingClubAreaRepository; }
@Override protected JpaRepository<GroupHuntingDay, Long> getRepository() { return huntingDayRepository; }
@Override protected JpaRepository<Occupation, Long> getRepository() { return occupationRepository; }
@Override protected JpaRepository<BasicClubHuntingSummary, Long> getRepository() { return basicClubHuntingSummaryRepository; }
@Override protected JpaRepository<MooseHuntingSummary, Long> getRepository() { return mooseHuntingSummaryRepository; }
@Override protected JpaRepository<MooseHarvestReport, Long> getRepository() { return mooseHarvestReportRepository; }
@Override protected JpaRepository<SystemUser, Long> getRepository() { return userRepository; }
@Override protected JpaRepository<HarvestPermitArea, Long> getRepository() { return harvestPermitAreaRepository; }
@Override protected JpaRepository<HarvestPermit, Long> getRepository() { return harvestPermitRepository; }
@Override protected JpaRepository<HarvestReport, Long> getRepository() { return harvestReportRepository; }
@Override protected JpaRepository<Riistanhoitoyhdistys, Long> getRepository() { return riistanhoitoyhdistysRepository; }
@Override protected JpaRepository<JHTTraining, Long> getRepository() { return jhtTrainingRepository; }
@Override protected JpaRepository<OccupationNomination, Long> getRepository() { return occupationNominationRepository; }
@Override protected JpaRepository<Venue, Long> getRepository() { return venueRepository; }
@Override protected JpaRepository<CalendarEvent, Long> getRepository() { return calendarEventRepository; }
@Override protected JpaRepository<Organisation, Long> getRepository() { return organisationRepository; }
public GenericController(JpaRepository<T, Long> repo){ this.repo = repo; }
@Autowired public EntityEditorPresenter(JpaRepository<T, Long> repository, EntityUtil entityUtil) { this.repository = repository; this.entityUtil = entityUtil; }
@Autowired public RoleEditorPresenter(JpaRepository<Role, Long> repository, EntityUtil entityUtil) { super(repository, entityUtil); }
@Autowired public UserEditorPresenter(JpaRepository<User, Long> repository, EntityUtil entityUtil) { super(repository, entityUtil); }
@Autowired public ProjectEditorPresenter(JpaRepository<Project, Long> repository, EntityUtil entityUtil) { super(repository, entityUtil); }
public TestPresenter(JpaRepository<TestBean, Long> repository, EntityUtil entityUtil) { super(repository, entityUtil); }
@Override protected JpaRepository<User, Long> getRepository() { return userRepository; }
@Override protected JpaRepository<Reply, Long> getRepository() { return replyRepository; }
@Override protected JpaRepository<Topic, Long> getRepository() { return topicRepository; }