001: package org.outerj.daisy.sync.mapping;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005: import java.util.logging.Level;
006:
007: import org.outerj.daisy.sync.Attribute;
008: import org.outerj.daisy.sync.AttributeImpl;
009: import org.outerj.daisy.sync.Entity;
010: import org.outerj.daisy.sync.VariantHelper;
011: import org.outerj.daisy.sync.dao.ExternalEntityDao;
012: import org.outerj.daisy.sync.dao.SyncEntityDao;
013: import org.outerj.daisy.sync.mapping.EntityMapping.MappedAttribute;
014:
015: public class AssociatedEntityMapping extends AbstractAssociatedMapping {
016: private String entityName;
017:
018: private String joinKey;
019:
020: private String joinParentKey;
021:
022: private List<AssociatedMapping> childMappings = new ArrayList<AssociatedMapping>();
023:
024: private AttributeMapping targetAttribute;
025:
026: public AssociatedEntityMapping(String entityName, String joinKey,
027: String joinParentKey, ExternalEntityDao externalEntityDao,
028: SyncEntityDao syncEntityDao,
029: MappingConfiguration configuration) {
030: super (externalEntityDao, syncEntityDao, configuration);
031: this .entityName = entityName;
032: this .joinKey = joinKey;
033: this .joinParentKey = joinParentKey;
034: }
035:
036: public void applyMapping(Entity originalEntity)
037: throws MappingException {
038: applyMapping(originalEntity, originalEntity);
039: }
040:
041: // joinParentKey here then this object should make use of the associatedEntity to fetch the key
042: public void applyMapping(Entity originalEntity,
043: Entity associatedEntity) throws MappingException {
044: List<Entity> associatedEntities = null;
045: if (joinKey != null) {
046: long extId = associatedEntity.getExternalId();
047: associatedEntities = externalEntityDao
048: .getAssociatedEntities(extId, entityName, joinKey);
049: } else if (joinParentKey != null) {
050: Attribute associatedAttr = associatedEntity
051: .getAttributeByExternalName(joinParentKey);
052: associatedEntities = new ArrayList<Entity>();
053: if (associatedAttr != null) {
054: for (String joinValue : associatedAttr.getValues()) {
055: List<Entity> entities = externalEntityDao
056: .getEntity(entityName, Long
057: .parseLong(joinValue));
058: if (entities != null)
059: associatedEntities.addAll(entities);
060: }
061: }
062: }
063:
064: for (Entity entity : associatedEntities) {
065: if (this .childMappings.size() > 0) {
066: for (AssociatedMapping childMapping : this .childMappings)
067: childMapping.applyMapping(originalEntity, entity);
068: } else {
069: // if this mapping doesn't have any children get the daisy id
070: // and store it in the attribute
071: // TODO check if entity exists first
072: try {
073: String internalEntityName = entity
074: .getInternalName();
075: if (internalEntityName == null)
076: internalEntityName = configuration
077: .getEntityMappingByEntityName(
078: entity.getName())
079: .getDaisyDocumentTypeName();
080:
081: Entity sourceEntity = syncEntityDao.getEntity(
082: internalEntityName, entity.getExternalId(),
083: originalEntity.getLanguage());
084:
085: Attribute attribute = originalEntity
086: .getAttributeByExternalName(targetAttribute
087: .getName());
088: if (attribute == null) {
089: // if it's not found then it must be created
090: attribute = new AttributeImpl(targetAttribute
091: .getName(), null, targetAttribute
092: .getDaisyName(), targetAttribute
093: .getType());
094: originalEntity.addAttribute(attribute);
095: }
096:
097: if (sourceEntity != null)
098: attribute.addValue(VariantHelper
099: .variantKeyToString(sourceEntity
100: .getDaisyVariantKey()));
101: else
102: logger
103: .logp(
104: Level.WARNING,
105: this .getClass().getName(),
106: "applyMapping",
107: "Could not map associated entity ({0} - {1}) since the source entity could not be found",
108: new Object[] {
109: entity.getName(),
110: entity.getExternalId() });
111: if (attribute instanceof MappedAttribute)
112: ((MappedAttribute) attribute).setMapped(true);
113: } catch (Exception e) {
114: logger.logp(Level.SEVERE,
115: this .getClass().getName(), "applyMapping",
116: "Error setting associated mapping for entity ("
117: + entity.getName() + " - "
118: + entity.getExternalId() + ")", e);
119: }
120: }
121: }
122:
123: }
124:
125: public AttributeMapping getTargetAttribute() {
126: return targetAttribute;
127: }
128:
129: public void setTargetAttribute(AttributeMapping attributeMapping) {
130: this .targetAttribute = attributeMapping;
131: for (AssociatedMapping childMapping : this .childMappings)
132: childMapping.setTargetAttribute(attributeMapping);
133: }
134:
135: public List<AssociatedMapping> getChildMappings() {
136: return childMappings;
137: }
138:
139: public void setChildMappings(List<AssociatedMapping> childMappings) {
140: this .childMappings = childMappings;
141: if (targetAttribute != null) {
142: for (AssociatedMapping childMapping : this .childMappings)
143: childMapping.setTargetAttribute(targetAttribute);
144: }
145: }
146:
147: public void addChildMapping(AssociatedMapping mapping) {
148: this .childMappings.add(mapping);
149: if (this .targetAttribute != null) {
150: mapping.setTargetAttribute(this .targetAttribute);
151: }
152: }
153:
154: public String getJoinKey() {
155: return joinKey;
156: }
157:
158: public String getJoinParentKey() {
159: return joinParentKey;
160: }
161:
162: public String getEntityName() {
163: return entityName;
164: }
165: }
|