Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 
 package org.apache.cayenne.modeler.dialog.objentity;
 
 import java.util.List;
 
 import  org.apache.log4j.Logger;
 import  org.apache.cayenne.CayenneRuntimeException;
 import  org.apache.cayenne.map.DbEntity;
 import  org.apache.cayenne.map.DbRelationship;
 import  org.apache.cayenne.map.Entity;
 import  org.apache.cayenne.map.ObjEntity;
 import  org.apache.cayenne.map.ObjRelationship;
 import  org.apache.cayenne.map.Relationship;
 import  org.apache.cayenne.util.Util;
 import  org.scopemvc.core.IntIndexSelector;
 import  org.scopemvc.core.ModelChangeEvent;
 import  org.scopemvc.core.ModelChangeTypes;
 import  org.scopemvc.core.Selector;
 import  org.scopemvc.model.basic.BasicModel;
 import  org.scopemvc.model.collection.ListModel;

A Scope model for mapping an ObjRelationship to one or more DbRelationships.

Author(s):
Andrei Adamchik
Since:
1.1
 
 public class ObjRelationshipInfoModel extends BasicModel {
     static final Logger logObj = Logger.getLogger(ObjRelationshipInfoModel.class);
 
 
     public static final Selector DB_RELATIONSHIP_PATH_SELECTOR =
         Selector.fromString("dbRelationshipPath");
     public static final Selector SOURCE_ENTITY_NAME_SELECTOR =
         Selector.fromString("relationship.sourceEntity.name");
     public static final Selector SELECTED_PATH_COMPONENT_SELECTOR =
         Selector.fromString("selectedPathComponent");
     public static final Selector OBJECT_TARGET_SELECTOR =
         Selector.fromString("objectTarget");
     public static final Selector OBJECT_TARGETS_SELECTOR =
         Selector.fromString("objectTargets");
     public static final Selector RELATIONSHIP_NAME_SELECTOR =
         Selector.fromString("relationshipName");
 
     protected ObjRelationship relationship;
     protected ListModel dbRelationshipPath;
     protected ObjEntity objectTarget;
     protected List objectTargets;
     protected String relationshipName;
 
     public ObjRelationshipInfoModel(
         ObjRelationship relationship,
         Collection objEntities) {
 
         this. = relationship;
         this. = relationship.getName();
         this. = (ObjEntity) relationship.getTargetEntity();
 
         // prepare entities - copy those that have DbEntities mapped, and then sort
 
         this. = new ArrayList(objEntities.size());
         Iterator entities = objEntities.iterator();
         while (entities.hasNext()) {
             ObjEntity entity = (ObjEntity) entities.next();
             if (entity.getDbEntity() != null) {
                 .add(entity);
             }
         }
 
         Collections.sort(, Comparators.getNamedObjectComparator());
 
         // validate -
         // current limitation is that an ObjRelationship must have source 
         // and target entities present, with DbEntities chosen.
        validateCanMap();
        // wrap path
        this. = new ListModel();
        Iterator it = relationship.getDbRelationships().iterator();
        while (it.hasNext()) {
            DbRelationship dbRelationship = (DbRelationship) it.next();
            this..add(new EntityRelationshipsModel(dbRelationship));
        }
        // add dummy last relationship if we are not connected
        connectEnds();
        this..addModelChangeListener(this);
    }
    public ObjRelationship getRelationship() {
        return ;
    }
    public ListModel getDbRelationshipPath() {
        return ;
    }
        return ;
    }
    public void setSelectedPathComponent(EntityRelationshipsModel selectedPathComponent) {
        if (this. != selectedPathComponent) {
            unlistenOldSubmodel();
            this. = selectedPathComponent;
            listenNewSubmodel();
            fireModelChange(
                ModelChangeTypes.VALUE_CHANGED,
                );
        }
    }

    
Returns currently selected target of the ObjRelationship.
    public ObjEntity getObjectTarget() {
        return ;
    }

    
Sets a new target
    public void setObjectTarget(ObjEntity objectTarget) {
        if (this. != objectTarget) {
            unlistenOldSubmodel();
            this. = objectTarget;
            listenNewSubmodel();
            fireModelChange(ModelChangeTypes.VALUE_CHANGED, );
            // change the list of relationships 
            breakChain(-1);
            connectEnds();
            fireModelChange(
                ModelChangeTypes.VALUE_CHANGED,
                );
        }
    }

    
Returns a list of ObjEntities available for target mapping.
    public List getObjectTargets() {
        return ;
    }
    public String getRelationshipName() {
        return ;
    }
    public void setRelationshipName(String relationshipName) {
        this. = relationshipName;
    }
    public void modelChanged(ModelChangeEvent event) {
        // if a different relationship was selected, we may need to rebuild the list
        Selector selector = event.getSelector();
        while (selector != null) {
            if (selector instanceof IntIndexSelector) {
                IntIndexSelector indexSel = (IntIndexSelector) selector;
                relationshipChanged(indexSel.getIndex());
                break;
            }
            selector = selector.getNext();
        }
        super.modelChanged(event);
    }

    
Processes relationship path when path component at index was changed.
    public synchronized void relationshipChanged(int index) {
        // strip everything starting from the index
        breakChain(index);
        // connect the ends
        connectEnds();
        
        // must fire with null selector, or refresh won't happen
        .fireModelChange(VALUE_CHANGED, null);
    }

    
Stores current state of the model in the internal ObjRelationship.
    public synchronized boolean savePath() {
        // check for modifications
        if (.getTargetEntity() == ) {
            if (Util.nullSafeEquals(.getName(), )) {
                List oldPath = .getDbRelationships();
                if (oldPath.size() == .size()) {
                    boolean hasChanges = false;
                    for (int i = 0; i < oldPath.size(); i++) {
                        EntityRelationshipsModel next =
                            (EntityRelationshipsModel.get(i);
                        if (oldPath.get(i) != next.getSelectedRelationship()) {
                            hasChanges = true;
                            break;
                        }
                    }
                    if (!hasChanges) {
                        return false;
                    }
                }
            }
        }
        // detected modifications, save...
        .clearDbRelationships();
        // note on events notification - this needs to be propagated
        // via old modeler events, but we leave this to the controller
        // since model knows nothing about Modeler mediator.
        .setTargetEntity();
        .setName();
        Iterator it = .iterator();
        while (it.hasNext()) {
            EntityRelationshipsModel next = (EntityRelationshipsModelit.next();
            Relationship nextPathComponent = next.getSelectedRelationship();
            if (nextPathComponent == null) {
                break;
            }
            .addDbRelationship((DbRelationship) nextPathComponent);
        }
        return true;
    }
    private void breakChain(int index) {
        // strip everything starting from the index
        .makeActive(false);
        try {
            while (.size() > (index + 1)) {
                // remove last
                .remove(.size() - 1);
            }
        }
        finally {
            .makeActive(true);
        }
    }
    // Connects last selected DbRelationship in the path to the 
    // last DbEntity, creating a dummy relationship if needed.
    private void connectEnds() {
        Relationship last = null;
        int size = .size();
        if (size > 0) {
            EntityRelationshipsModel wrapper =
                (EntityRelationshipsModel.get(size - 1);
            last = wrapper.getSelectedRelationship();
        }
        Entity target = getEndEntity();
        if (last == null || last.getTargetEntity() != target) {
            // try to connect automatically, if we can't use dummy connector
            Entity source = (last == null) ? getStartEntity() : last.getTargetEntity();
            Relationship anyConnector = source.getAnyRelationship(target);
            EntityRelationshipsModel connector = null;
            connector =
                (anyConnector == null)
                    ? new EntityRelationshipsModel(sourcegetEndEntity())
                    : new EntityRelationshipsModel(anyConnector);
            .makeActive(false);
            try {
                .add(connector);
            }
            finally {
                .makeActive(true);
            }
        }
    }
    private void validateCanMap() {
        if (.getSourceEntity() == null) {
            throw new CayenneRuntimeException("Can't map relationship without source entity.");
        }
        if (.getTargetEntity() == null) {
            throw new CayenneRuntimeException("Can't map relationship without target entity.");
        }
        if (getStartEntity() == null) {
            throw new CayenneRuntimeException("Can't map relationship without source DbEntity.");
        }
        if (getEndEntity() == null) {
            throw new CayenneRuntimeException("Can't map relationship without target DbEntity.");
        }
    }
    public DbEntity getStartEntity() {
        return ((ObjEntity) .getSourceEntity()).getDbEntity();
    }
    public DbEntity getEndEntity() {
        return .getDbEntity();
    }
New to GrepCode? Check out our FAQ X