|
| 1 | +package org.openforis.collect.io.data.csv; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.openforis.collect.io.data.csv.columnProviders.AutomaticColumnProvider; |
| 7 | +import org.openforis.collect.io.data.csv.columnProviders.ColumnProvider; |
| 8 | +import org.openforis.collect.io.data.csv.columnProviders.ColumnProviderChain; |
| 9 | +import org.openforis.collect.io.data.csv.columnProviders.ColumnProviders; |
| 10 | +import org.openforis.idm.metamodel.AttributeDefinition; |
| 11 | +import org.openforis.idm.metamodel.EntityDefinition; |
| 12 | +import org.openforis.idm.model.Entity; |
| 13 | +import org.openforis.idm.model.Node; |
| 14 | + |
| 15 | +/** |
| 16 | + * @author S. Ricci |
| 17 | + */ |
| 18 | +public class MultipleEntityColumnProvider extends ColumnProviderChain { |
| 19 | + |
| 20 | + private static final int DEFAULT_MAX_ITEMS = 10; |
| 21 | + |
| 22 | + |
| 23 | + public MultipleEntityColumnProvider(CSVDataExportParameters config, EntityDefinition defn) { |
| 24 | + super(config, defn, createProviders(config, defn)); |
| 25 | + } |
| 26 | + |
| 27 | + private static List<ColumnProvider> createProviders(CSVDataExportParameters config, EntityDefinition defn) { |
| 28 | + List<ColumnProvider> providers = new ArrayList<ColumnProvider>(); |
| 29 | + List<AttributeDefinition> keyDefs = defn.getKeyAttributeDefinitions(); |
| 30 | + int maxEntities = defn.getFixedMaxCount() == null ? DEFAULT_MAX_ITEMS: defn.getFixedMaxCount(); |
| 31 | + for (int entityIndex = 0; entityIndex < maxEntities; entityIndex ++) { |
| 32 | + for (AttributeDefinition keyDef : keyDefs) { |
| 33 | + String keyName = keyDef.getName(); |
| 34 | + MultipleEntityKeyColumnProvider p = new MultipleEntityKeyColumnProvider(config, defn, entityIndex, keyName); |
| 35 | + providers.add(p); |
| 36 | + } |
| 37 | + } |
| 38 | + return providers; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + protected String generateHeadingPrefix() { |
| 43 | + if (entityDefinition == null) { |
| 44 | + throw new IllegalStateException("Entity definition not specified for multiple entity column provider"); |
| 45 | + } |
| 46 | + return ColumnProviders.generateHeadingPrefix(entityDefinition, config); |
| 47 | + } |
| 48 | + |
| 49 | + private static class MultipleEntityKeyColumnProvider extends AutomaticColumnProvider { |
| 50 | + |
| 51 | + private String keyName; |
| 52 | + private int entityIndex; |
| 53 | + |
| 54 | + public MultipleEntityKeyColumnProvider(CSVDataExportParameters config, EntityDefinition defn, int entityIndex, String keyName) { |
| 55 | + super(config, "[" + (entityIndex + 1) + "]_", defn); |
| 56 | + this.entityIndex = entityIndex; |
| 57 | + this.keyName = keyName; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public List<Object> extractValues(Node<?> axis) { |
| 62 | + Entity parentEntity = (Entity) axis; |
| 63 | + List<Node<?>> children = parentEntity.getChildren(entityDefinition); |
| 64 | + Node<?> childEntity = entityIndex < children.size() ? children.get(entityIndex) : null; |
| 65 | + if (childEntity == null) { |
| 66 | + // not found; return empty array |
| 67 | + return super.emptyValues(); |
| 68 | + } |
| 69 | + return super.extractValues(childEntity); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments