/* * DBeaver - Universal Database Manager * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) * * Licensed 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. */ /* * Created on Jul 15, 2004 */ package org.jkiss.dbeaver.ext.erd.command; import org.eclipse.gef.commands.Command; import org.jkiss.dbeaver.ext.erd.model.ERDAssociation; import org.jkiss.dbeaver.ext.erd.model.ERDEntity; import java.util.List; /** * Command to change the primary key we are connecting to a particular foreign key * key * * @author Serge Rider */ public class AssociationReconnectTargetCommand extends Command { /** source Table * */ protected ERDEntity sourceForeignKey; /** target Table * */ protected ERDEntity targetPrimaryKey; /** Relationship between source and target * */ protected ERDAssociation relationship; /** previous source prior to command execution * */ protected ERDEntity oldTargetPrimaryKey; /** * Makes sure that foreign key doesn't reconnect to itself or try to create * a relationship which already exists */ @Override public boolean canExecute() { boolean returnVal = true; ERDEntity foreignKeyEntity = relationship.getForeignKeyEntity(); if (foreignKeyEntity.equals(targetPrimaryKey)) { returnVal = false; } else { List<?> relationships = targetPrimaryKey.getPrimaryKeyRelationships(); for (int i = 0; i < relationships.size(); i++) { ERDAssociation relationship = ((ERDAssociation) (relationships.get(i))); if (relationship.getForeignKeyEntity().equals(sourceForeignKey) && relationship.getPrimaryKeyEntity().equals(targetPrimaryKey)) { returnVal = false; break; } } } return returnVal; } /** * @see org.eclipse.gef.commands.Command#execute() */ @Override public void execute() { if (targetPrimaryKey != null) { oldTargetPrimaryKey.removePrimaryKeyRelationship(relationship, true); relationship.setPrimaryKeyEntity(targetPrimaryKey); targetPrimaryKey.addPrimaryKeyRelationship(relationship, true); } } /** * @return Returns the sourceForeignKey. */ public ERDEntity getSourceForeignKey() { return sourceForeignKey; } /** * @param sourceForeignKey * The sourceForeignKey to set. */ public void setSourceForeignKey(ERDEntity sourceForeignKey) { this.sourceForeignKey = sourceForeignKey; } /** * @return Returns the targetPrimaryKey. */ public ERDEntity getTargetPrimaryKey() { return targetPrimaryKey; } /** * @param targetPrimaryKey * The targetPrimaryKey to set. */ public void setTargetPrimaryKey(ERDEntity targetPrimaryKey) { this.targetPrimaryKey = targetPrimaryKey; } /** * @return Returns the relationship. */ public ERDAssociation getRelationship() { return relationship; } /** * Sets the Relationship associated with this * * @param relationship * the Relationship */ public void setRelationship(ERDAssociation relationship) { this.relationship = relationship; oldTargetPrimaryKey = relationship.getPrimaryKeyEntity(); sourceForeignKey = relationship.getForeignKeyEntity(); } /** * @see org.eclipse.gef.commands.Command#undo() */ @Override public void undo() { targetPrimaryKey.removePrimaryKeyRelationship(relationship, true); relationship.setPrimaryKeyEntity(oldTargetPrimaryKey); oldTargetPrimaryKey.addPrimaryKeyRelationship(relationship, true); } }