001package systems.dmx.core.impl; 002 003import systems.dmx.core.model.AssociationDefinitionModel; 004import systems.dmx.core.model.SimpleValue; 005import systems.dmx.core.model.TopicModel; 006 007import java.util.ArrayList; 008import java.util.Iterator; 009import java.util.List; 010import java.util.logging.Logger; 011 012 013 014class LabelCalculation { 015 016 // ------------------------------------------------------------------------------------------------------- Constants 017 018 private static final String LABEL_CHILD_SEPARATOR = " "; 019 private static final String LABEL_TOPIC_SEPARATOR = ", "; 020 021 // ---------------------------------------------------------------------------------------------- Instance Variables 022 023 private DMXObjectModelImpl comp; 024 private List<String> labelAssocDefUris; 025 026 private Logger logger = Logger.getLogger(getClass().getName()); 027 028 // ---------------------------------------------------------------------------------------------------- Constructors 029 030 /** 031 * Preconditions: 032 * - comp is not null 033 * - comp is composite 034 * 035 * @param comp A composite. 036 */ 037 LabelCalculation(DMXObjectModelImpl comp) { 038 this.comp = comp; 039 this.labelAssocDefUris = comp.getType().getLabelAssocDefUris(); 040 } 041 042 // ----------------------------------------------------------------------------------------- Package Private Methods 043 044 void calculate() { 045 try { 046 StringBuilder builder = new StringBuilder(); 047 for (String assocDefUri : labelAssocDefUris) { 048 comp.loadChildTopics(assocDefUri, false); // deep=false, FIXME? 049 appendLabel(calculateChildLabel(assocDefUri), builder, LABEL_CHILD_SEPARATOR); 050 } 051 // 052 comp._updateSimpleValue(new SimpleValue(builder.toString())); 053 } catch (Exception e) { 054 throw new RuntimeException("Calculating and updating label of " + comp.objectInfo() + 055 " failed (assoc defs involved: " + labelAssocDefUris + ")", e); 056 } 057 } 058 059 // ------------------------------------------------------------------------------------------------- Private Methods 060 061 private String calculateChildLabel(String assocDefUri) { 062 Object value = comp.getChildTopicsModel().get(assocDefUri); 063 // Note: topics just created have no child topics yet 064 if (value == null) { 065 return ""; 066 } 067 // 068 if (value instanceof TopicModel) { 069 // single value 070 return ((TopicModel) value).getSimpleValue().toString(); 071 } else if (value instanceof List) { 072 // multiple value 073 StringBuilder builder = new StringBuilder(); 074 for (TopicModel childTopic : (List<TopicModel>) value) { 075 appendLabel(childTopic.getSimpleValue().toString(), builder, LABEL_TOPIC_SEPARATOR); 076 } 077 return builder.toString(); 078 } else { 079 throw new RuntimeException("Unexpected value in a ChildTopicsModel: " + value); 080 } 081 } 082 083 private void appendLabel(String label, StringBuilder builder, String separator) { 084 // add separator 085 if (builder.length() > 0 && label.length() > 0) { 086 builder.append(separator); 087 } 088 // 089 builder.append(label); 090 } 091}