001    package com.poemspace.dm4;
002    
003    import java.util.ArrayList;
004    import java.util.Collection;
005    import java.util.HashMap;
006    import java.util.List;
007    import java.util.Map;
008    import java.util.logging.Logger;
009    
010    import de.deepamehta.core.AssociationDefinition;
011    import de.deepamehta.core.RelatedTopic;
012    import de.deepamehta.core.Topic;
013    import de.deepamehta.core.TopicType;
014    import de.deepamehta.core.service.DeepaMehtaService;
015    
016    public class CriteriaCache {
017    
018        private static Logger log = Logger.getLogger(CriteriaCache.class.getName());
019    
020        private List<Topic> types = null;
021        private List<String> typeUris = null;
022    
023        private final DeepaMehtaService dms;
024    
025        public CriteriaCache(DeepaMehtaService dms) {
026            this.dms = dms;
027        }
028    
029        /**
030         * Returns all criteria types.
031         * 
032         * @return list of all criteria associated type topics
033         */
034        public List<Topic> getTypes() {
035            if (types == null) {
036                types = new ArrayList<Topic>();
037    
038                log.info("reveal criteria types");
039                Map<String, Topic> typesByUri = new HashMap<String, Topic>();
040                TopicType criteriaType = dms.getTopicType("dm4.poemspace.criteria.type");
041                for (RelatedTopic type : criteriaType.getRelatedTopics("dm4.core.association", null,
042                        null, null, false, false, 0)) {
043                    typesByUri.put(type.getUri(), type);
044                }
045    
046                // use order of person aggregates
047                TopicType personType = dms.getTopicType("dm4.contacts.person");
048                Collection<AssociationDefinition> assocDefs = personType.getAssocDefs();
049    
050                for (AssociationDefinition assocDef : assocDefs) {
051                    if (assocDef.getTypeUri().equals("dm4.core.aggregation_def")) {
052                        if (typesByUri.containsKey(assocDef.getChildTypeUri())) {
053                            log.info("use criteria uri " + assocDef.getChildTypeUri());
054                            types.add(typesByUri.get(assocDef.getChildTypeUri()));
055                        }
056                    }
057                }
058            }
059            return types;
060        }
061    
062        /**
063         * Returns all criteria type URIs.
064         * 
065         * @return list of all criteria associated type topic URIs
066         */
067        public List<String> getTypeUris() {
068            if (typeUris == null) {
069                log.info("reveal criteria type URIs");
070                typeUris = new ArrayList<String>();
071                for (Topic type : getTypes()) {
072                    typeUris.add(type.getUri());
073                }
074            }
075            return typeUris;
076        }
077    
078    }