/*
 * 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.ode.dao.jpa;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Query;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.ode.bpel.dao.CorrelationSetDAO;
import org.apache.ode.bpel.dao.PartnerLinkDAO;
import org.apache.ode.bpel.dao.ProcessInstanceDAO;
import org.apache.ode.bpel.dao.ScopeDAO;
import org.apache.ode.bpel.dao.ScopeStateEnum;
import org.apache.ode.bpel.dao.XmlDataDAO;
import org.apache.ode.bpel.evt.BpelEvent;

/**
 * @author dhanukar
 *
 */
@Entity
@Table(name="ODE_SCOPE")
@NamedQueries({
    @NamedQuery(name="ScopeEvents", query="SELECT DISTINCT se FROM EventDAOImpl  se LEFT JOIN FETCH se._event  WHERE se._scopeId = :sid"),
    @NamedQuery(name=ScopeDAOImpl.SELECT_SCOPE_IDS_BY_PROCESS, query="select s._scopeInstanceId from ScopeDAOImpl as s where s._processInstance._process = :process"),
    @NamedQuery(name=ScopeDAOImpl.SELECT_SCOPE_IDS_BY_INSTANCE, query="select s._scopeInstanceId from ScopeDAOImpl as s where s._processInstance = :instance"),
    @NamedQuery(name=ScopeDAOImpl.DELETE_SCOPES_BY_SCOPE_IDS, query="delete from ScopeDAOImpl as s where s._scopeInstanceId in(:ids)")
})
/*@FetchGroups({
    @FetchGroup(name="detail", attributes={
        @FetchAttribute(name="_parentScope"),
        @FetchAttribute(name="_childScopes")
    })})*/
public class ScopeDAOImpl extends OpenJPADAO implements ScopeDAO {
    public final static String SELECT_SCOPE_IDS_BY_PROCESS = "SELECT_SCOPE_IDS_BY_PROCESS";
    public final static String SELECT_SCOPE_IDS_BY_INSTANCE = "SELECT_SCOPE_IDS_BY_INSTANCE";
    public final static String DELETE_SCOPES_BY_SCOPE_IDS = "DELETE_SCOPES_BY_SCOPE_IDS";

    @Id @Column(name="SCOPE_ID")
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Long _scopeInstanceId;

    @Basic @Column(name="MODEL_ID")
    private int _modelId;
    @Basic @Column(name="SCOPE_NAME")
    private String _name;
    @Basic @Column(name="SCOPE_STATE")
    private String _scopeState;

    @ManyToOne(fetch=FetchType.EAGER,cascade={CascadeType.PERSIST})
    @Column(name="PARENT_SCOPE_ID")
    private ScopeDAOImpl _parentScope;

    @OneToMany(targetEntity=ScopeDAOImpl.class,mappedBy="_parentScope",fetch=FetchType.LAZY,cascade={CascadeType.ALL})
    private Collection<ScopeDAO> _childScopes = new ArrayList<ScopeDAO>();
    @OneToMany(targetEntity=CorrelationSetDAOImpl.class,mappedBy="_scope",fetch=FetchType.LAZY,cascade={CascadeType.ALL})
    private Collection<CorrelationSetDAO> _correlationSets = new ArrayList<CorrelationSetDAO>();
    @OneToMany(targetEntity=PartnerLinkDAOImpl.class,mappedBy="_scope",fetch= FetchType.LAZY,cascade={CascadeType.MERGE, CascadeType.REFRESH, CascadeType.PERSIST, CascadeType.DETACH})
    private Collection<PartnerLinkDAO> _partnerLinks = new ArrayList<PartnerLinkDAO>();
    @OneToMany(targetEntity=XmlDataDAOImpl.class,mappedBy="_scope",fetch=FetchType.LAZY,cascade={CascadeType.MERGE, CascadeType.REFRESH, CascadeType.PERSIST, CascadeType.DETACH})
    private Collection<XmlDataDAO> _variables = new ArrayList<XmlDataDAO>();
    @ManyToOne(fetch=FetchType.EAGER,cascade={CascadeType.PERSIST}) @Column(name="PROCESS_INSTANCE_ID")
    private ProcessInstanceDAOImpl _processInstance;
    
    @Transient
    private Set<EventDAOImpl> events = new HashSet<EventDAOImpl>();
    @Transient
    private Set<CorrelationSetDAO> correlationDTOs = new HashSet<CorrelationSetDAO>();
    @Transient 
    private Set<XmlDataDAO> variablesDTOs = new HashSet<XmlDataDAO>();

    public ScopeDAOImpl() {}
    public ScopeDAOImpl(ScopeDAOImpl parentScope, String name, int scopeModelId, ProcessInstanceDAOImpl pi) {
        _parentScope = parentScope;
        _name = name;
        _modelId = scopeModelId;
        _processInstance = pi;
    }

    public PartnerLinkDAO createPartnerLink(int plinkModelId, String pLinkName,
            String myRole, String partnerRole) {
        PartnerLinkDAOImpl pl = new PartnerLinkDAOImpl(plinkModelId, pLinkName, myRole, partnerRole);
        pl.setScope(this);
        _partnerLinks.add(pl);
        return pl;
    }

    public Collection<ScopeDAO> getChildScopes() {
        return _childScopes;
    }
    
    public void setChildScopes(Collection<ScopeDAO> childrens){
    	_childScopes = childrens;
    }
    
    public boolean isChildrenExist(){
    	return _childScopes.isEmpty() ? false : true;
    }

    public CorrelationSetDAO getCorrelationSet(String corrSetName) {
        CorrelationSetDAO ret = null;
        for (CorrelationSetDAO csElement : _correlationSets) {
            if ( csElement.getName().equals(corrSetName)) ret = csElement;
        }

        if ( ret == null ) {
            // Apparently the caller knows there should be a correlation set
            // in here. Create a new set if one does not exist.
            // Not sure I understand this implied object creation and why
            // an explicit create pattern isn't used ( i.e. similar to
            // PartnerLink creation )
            ret = new CorrelationSetDAOImpl(this,corrSetName);
            // Persist the new correlation set to generate an ID
            getEM().persist(ret);
            _correlationSets.add(ret);
        }

        return ret;
    }

    public Collection<CorrelationSetDAO> getCorrelationSets() {
        return _correlationSets;
    }
    
    public void setCorrelationSets(List<CorrelationSetDAO> correlationSet){
    	this._correlationSets = correlationSet;
    } 

    public int getModelId() {
        return _modelId;
    }

    public String getName() {
        return _name;
    }

    public ScopeDAO getParentScope() {
        return _parentScope;
    }

    public PartnerLinkDAO getPartnerLink(int plinkModelId) {
        for (PartnerLinkDAO pLink : getPartnerLinks()) {
            if (pLink.getPartnerLinkModelId() == plinkModelId) {
                return pLink;
            }
        }
        return null;
    }

    public Collection<PartnerLinkDAO> getPartnerLinks() {
        return _partnerLinks;
    }

    public ProcessInstanceDAO getProcessInstance() {
        return _processInstance;
    }

    public Long getScopeInstanceId() {
        return _scopeInstanceId;
    }

    public ScopeStateEnum getState() {
        return ScopeStateEnum.valueOf(_scopeState);
    }

    public XmlDataDAO getVariable(String varName) {
        XmlDataDAO ret = null;

        for (XmlDataDAO xmlElement : _variables) {
            if ( xmlElement.getName().equals(varName)) return xmlElement;
        }

        ret = new XmlDataDAOImpl(this,varName);
        _variables.add(ret);

        return ret;
    }

    public Collection<XmlDataDAO> getVariables() {
        return _variables;
    }
    
    public void setVariables(List<XmlDataDAO> variables){
    	this._variables = variables;
    }

    public List<BpelEvent> listEvents() {
        List<BpelEvent> result = new ArrayList<BpelEvent>();
        Query qry = getEM().createNamedQuery("ScopeEvents");
        qry.setParameter("sid", _scopeInstanceId);
        for (Object eventDao : qry.getResultList()) {
            result.add(((EventDAOImpl)eventDao).getEvent());
        }
        return result;
    }

    public void setState(ScopeStateEnum state) {
        _scopeState = state.toString();
    }
    
    
    
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime
				* result
				+ ((_scopeInstanceId == null) ? 0 : _scopeInstanceId.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ScopeDAOImpl other = (ScopeDAOImpl) obj;
		if (_scopeInstanceId == null) {
			if (other._scopeInstanceId != null)
				return false;
		} else if (!_scopeInstanceId.equals(other._scopeInstanceId))
			return false;
		return true;
	}
	
	@Override
	public String toString() {
		return "ScopeDAOImpl [_scopeInstanceId=" + _scopeInstanceId
				+ ", _name=" + _name + ", _parentScope=" + _parentScope + "]";
	}

	/**
	 * Use only when load parent eagerly
	 * @return
	 */
	public boolean isParentExist(){
		return (_parentScope == null ? false : true);
	}
	
	
	/**
	 * These are transient objects
	 * @return
	 */
	public Set<EventDAOImpl> getEvents() {
		return events;
	}
	
	/**
	 * These are transient objects
	 * @return
	 */
	public Set<CorrelationSetDAO> getCorrelationDTOs() {
		return correlationDTOs;
	}
	
	public void setCorrelationDTOs(Set<CorrelationSetDAO> correlationDTOs) {
		this.correlationDTOs = correlationDTOs;
	}
	
	/**
	 * These are transient objects
	 * @return
	 */
	public Set<XmlDataDAO> getVariablesDTOs() {
		return variablesDTOs;
	}
	
	public void setVariablesDTOs(Set<XmlDataDAO> variablesDTOs) {
		this.variablesDTOs = variablesDTOs;
	}
	
	public void addCorrelationSet(){
		correlationDTOs.addAll(_correlationSets);
	}
	
	
    
}
