Clover icon

sunshower-base

  1. Project Clover database Fri Apr 6 2018 03:41:27 UTC
  2. Package io.sunshower.persistence

File PersistenceUnit.java

 

Coverage histogram

../../../img/srcFileCovDistChart1.png
48% of files have more coverage

Code metrics

2
24
8
1
80
70
10
0.42
3
8
1.25

Classes

Class Line # Actions
PersistenceUnit 11 24 10
0.058823535.9%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    package io.sunshower.persistence;
2   
3    import io.sunshower.persistence.annotations.Persistence;
4    import java.util.*;
5    import java.util.stream.Collectors;
6    import org.springframework.beans.BeansException;
7    import org.springframework.beans.factory.NoSuchBeanDefinitionException;
8    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
9    import org.springframework.context.ApplicationContext;
10   
 
11    public class PersistenceUnit {
12    private final Dialect dialect;
13    private final Map<String, PersistenceConfiguration> configurations;
14   
 
15  0 toggle public PersistenceUnit(final Dialect dialect, ApplicationContext context) {
16  0 this.dialect = dialect;
17  0 configurations = new HashMap<>();
18  0 configure((ConfigurableListableBeanFactory) context.getAutowireCapableBeanFactory());
19    }
20   
 
21  0 toggle public String[] getScannedPackages() {
22  0 return configurations
23    .values()
24    .stream()
25    .flatMap(cfg -> cfg.packagesToScan.stream())
26    .collect(Collectors.toSet())
27    .toArray(new String[0]);
28    }
29   
 
30  0 toggle public Set<PersistenceConfiguration> configurations() {
31  0 return new HashSet<>(configurations.values());
32    }
33   
 
34  0 toggle public void addConfiguration(PersistenceConfiguration cfg) {
35  0 final PersistenceConfiguration existing = configurations.get(cfg.getId());
36  0 if (existing != null) {
37  0 existing.merge(cfg);
38    } else {
39  0 configurations.put(cfg.getId(), cfg);
40    }
41    }
42   
 
43  0 toggle private void configure(ConfigurableListableBeanFactory beanFactory) throws BeansException {
44  0 final String[] beanNamesForAnnotation =
45    beanFactory.getBeanNamesForAnnotation(Persistence.class);
46  0 for (String beanName : beanNamesForAnnotation) {
47  0 try {
48  0 final Set<String> packageNames = new HashSet<>();
49  0 final Set<String> migrationPaths = new HashSet<>();
50  0 final Set<Class<?>> entityTypes = new HashSet<>();
51  0 final Persistence persistence =
52    beanFactory.findAnnotationOnBean(beanName, Persistence.class);
53  0 include(packageNames, migrationPaths, entityTypes, persistence);
54  0 final PersistenceConfiguration cfg =
55    new PersistenceConfiguration(
56    persistence.id(), persistence.schema(), migrationPaths, packageNames, entityTypes);
57  0 addConfiguration(cfg);
58    } catch (NoSuchBeanDefinitionException ex) {
59    }
60    }
61    }
62   
 
63  0 toggle private void include(
64    Set<String> packageNames,
65    Set<String> migrationPaths,
66    Set<Class<?>> entityTypes,
67    Persistence type) {
68  0 packageNames.addAll(Arrays.asList(type.scannedPackages()));
69  0 migrationPaths.addAll(locations(type.migrationLocations()));
70  0 entityTypes.addAll(Arrays.asList(type.entities()));
71    }
72   
 
73  0 toggle private List<String> locations(String[] strings) {
74  0 return Arrays.stream(strings).map(t -> parseLocation(t, dialect)).collect(Collectors.toList());
75    }
76   
 
77  2 toggle public static String parseLocation(String s, Dialect dialect) {
78  2 return s.replaceAll("\\{dialect}", dialect.key);
79    }
80    }