All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.drools.kproject.KProjectChangeLog Maven / Gradle / Ivy

package org.drools.kproject;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class KProjectChangeLog
        implements
        PropertyChangeListener {

    private boolean        kProjectDirty;

    private Set    addedKBases;
    private Set    removedKBases;

    private Set    removedKSessions;
    private Set    addedKSessions;
    
    private Map kSessions;
    private Map    kBases;

    public KProjectChangeLog() {
        reset();
    }

    public boolean isKProjectDirty() {
        return kProjectDirty;
    }

    public void setKProjectDirty(boolean kProjectDirty) {
        this.kProjectDirty = kProjectDirty;
    }

    public boolean iskProjectDirty() {
        return kProjectDirty;
    }

    public void setkProjectDirty(boolean kProjectDirty) {
        this.kProjectDirty = kProjectDirty;
    }

    public Set getAddedKBases() {
        return addedKBases;
    }

    public void setAddedKBases(Set addedKBases) {
        this.addedKBases = addedKBases;
    }

    public Set getRemovedKBases() {
        return removedKBases;
    }

    public void setRemovedKBases(Set removedKBases) {
        this.removedKBases = removedKBases;
    }

    public Set getRemovedKSessions() {
        return removedKSessions;
    }

    public void setRemovedKSessions(Set removedKSessions) {
        this.removedKSessions = removedKSessions;
    }

    public Set getAddedKSessions() {
        return addedKSessions;
    }

    public void setAddedKSessions(Set addedKSessions) {
        this.addedKSessions = addedKSessions;
    }

    public Map getKSessions() {
        return kSessions;
    }

    public void setKSessions(Map kSessions) {
        this.kSessions = kSessions;
    }

    public Map getKBases() {
        return kBases;
    }

    public void setKBases(Map kBases) {
        this.kBases = kBases;
    }

    public void propertyChange(PropertyChangeEvent evt) {
        if ( evt.getSource() instanceof KProjectImpl ) {
            KProject kProject = (KProject) evt.getSource();
            if ( "kBases".equals( evt.getPropertyName() ) ) {
                Map oldKBases = (Map) evt.getOldValue();
                Map newKBases = (Map) evt.getNewValue();
                if ( oldKBases.size() < newKBases.size() ) {
                    // kBase added
                    for ( Entry entry : newKBases.entrySet() ) {
                        if ( !oldKBases.containsKey( entry.getKey() ) ) {
                            removedKBases.remove( entry.getKey() );               
                            addedKBases.add( entry.getKey() );
                            kBases.put( entry.getKey(), newKBases.get( entry.getKey() ) );
                            return;
                        }
                    }
                    throw new IllegalStateException( "Maps are different sizes, yet we can't find the new KBase" );
                } else if ( oldKBases.size() > newKBases.size() ) {
                    // kBase removed
                    for ( Entry entry : oldKBases.entrySet() ) {
                        if ( !newKBases.containsKey( entry.getKey() ) ) {
                            addedKBases.remove( entry.getKey() );
                            removedKBases.add( entry.getKey() );
                            kBases.put( entry.getKey(), oldKBases.get( entry.getKey() ) );
                            return;
                        }
                    }
                    throw new IllegalStateException( "Maps are different sizes, yet we can't find the removed KBase" );
                }
            } else {
                kProjectDirty = true;
            }

        } else if ( evt.getSource() instanceof KBaseImpl ) {
            KBaseImpl kBase = (KBaseImpl) evt.getSource();
            if ( "kSessions".equals( evt.getPropertyName() ) ) {
                Map oldKSession = (Map) evt.getOldValue();
                Map newKSession = (Map) evt.getNewValue();
                if ( oldKSession.size() < newKSession.size() ) {
                    // KSession added
                    for ( Entry entry : newKSession.entrySet() ) {
                        if ( !oldKSession.containsKey( entry.getKey() ) ) {
                            removedKSessions.remove( entry.getKey() );
                            addedKSessions.add( entry.getKey() );
                            kSessions.put( entry.getKey(), newKSession.get( entry.getKey() ) );
                            return;
                        }
                    }
                    throw new IllegalStateException( "Maps are different sizes, yet we can't find the new KBase" );
                } else if ( oldKSession.size() > newKSession.size() ) {
                    // KSession removed
                    for ( Entry entry : oldKSession.entrySet() ) {
                        if ( !newKSession.containsKey( entry.getKey() ) ) {
                            addedKSessions.remove( entry.getKey() ); 
                            removedKSessions.add( entry.getKey() );
                            kSessions.put( entry.getKey(), oldKSession.get( entry.getKey() ) );
                            return;
                        }
                    }
                    throw new IllegalStateException( "Maps are different sizes, yet we can't find the removed KBase" );
                }
            } else if ( "namespace".equals( evt.getPropertyName() ) ) {
                String oldV = (String) evt.getOldValue();
                String newV = (String) evt.getNewValue();

                String oldQName = oldV + "." + kBase.getName();
                String newQName = newV + "." + kBase.getName();
                
                kBase.getKProject().moveKBase( oldQName, newQName );

                removedKBases.remove( newQName );
                removedKBases.add( oldQName );
                addedKBases.remove( oldQName );
                addedKBases.add( newQName );
                kBases.put( newQName, kBase );
            } else if ( "name".equals( evt.getPropertyName() ) ) {
                String oldV = (String) evt.getOldValue();
                String newV = (String) evt.getNewValue();

                String oldQName = kBase.getNamespace() + "." + oldV;
                String newQName = kBase.getNamespace() + "." + newV;
                
                kBase.getKProject().moveKBase( oldQName, newQName );

                removedKBases.remove( newQName );                
                removedKBases.add( oldQName );
                addedKBases.remove( oldQName );
                addedKBases.add( newQName );
                kBases.put( newQName, kBase );
            } else {
                addedKBases.add( kBase.getQName() );
            }

        } else if ( evt.getSource() instanceof KSessionImpl ) {
            KSessionImpl kSession = (KSessionImpl) evt.getSource();
            if ( "namespace".equals( evt.getPropertyName() ) ) {
                String oldV = (String) evt.getOldValue();
                String newV = (String) evt.getNewValue();

                String oldQName = oldV + "." + kSession.getName();
                String newQName = newV + "." + kSession.getName();
                
                kSession.getKBase().moveKSession( oldQName, newQName );

                removedKSessions.remove( newQName );
                removedKSessions.add( oldQName );
                addedKSessions.remove( oldQName );
                addedKSessions.add( newQName );
                kSessions.put( newQName, kSession );
            } else if ( "name".equals( evt.getPropertyName() ) ) {
                String oldV = (String) evt.getOldValue();
                String newV = (String) evt.getNewValue();

                String oldQName = kSession.getNamespace() + "." + oldV;
                String newQName = kSession.getNamespace() + "." + newV;
                
                kSession.getKBase().moveKSession( oldQName, newQName );

                removedKSessions.remove( newQName );
                removedKSessions.add( oldQName );
                addedKSessions.remove( oldQName );
                addedKSessions.add( newQName );
                kSessions.put( newQName, kSession );
            } else {
                addedKSessions.add( kSession.getQName() );
                kSessions.put( kSession.getQName() , kSession );
            }
        }
    }

    public void reset() {
        kProjectDirty = false;
        removedKBases = new HashSet();
        addedKBases = new HashSet();

        removedKSessions = new HashSet();
        addedKSessions = new HashSet();
        
        kBases = new HashMap();
        kSessions = new HashMap();
        
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy