/**
 * Licensed to the Austrian Association for Software Tool Integration (AASTI)
 * under one or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information regarding copyright
 * ownership. The AASTI 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.openengsb.opencit.core.projectmanager.model;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.ObjectUtils;
import org.openengsb.core.api.model.ConnectorId;

@SuppressWarnings("serial")
public class Project implements Serializable {

    private Map<String, ConnectorId> services;
    private Map<String, ConnectorConfig> connectorConfigs;

    public enum State {
            OK,
            FAILURE;
    }

    private State state;
    private String id;
    private String notificationRecipient;
    private Date lastScmPollDate;

    public Project() {

    }

    public Project(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public void setNotificationRecipient(String notificationRecipient) {
        this.notificationRecipient = notificationRecipient;
    }

    public String getNotificationRecipient() {
        return notificationRecipient;
    }

    public State getState() {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }

    public Date getLastScmPollDate() {
        return this.lastScmPollDate;
    }

    public void setLastScmPollDate(Date lastScmPollDate) {
        this.lastScmPollDate = lastScmPollDate;
    }

    /**
     *
     * the created services, key is the type, and value the id of the service
     */
    public Map<String, ConnectorId> getServices() {
        return services;
    }

    public void addService(String type, ConnectorId connectorId) {
        if (services == null) {
            services = new HashMap<String, ConnectorId>();
        }
        services.put(type, connectorId);
    }

    public Map<String, ConnectorConfig> getConnectorConfigs() {
        return connectorConfigs;
    }

    public void addConnectorConfig(String type, ConnectorConfig attribs) {
        if (connectorConfigs == null) {
            connectorConfigs = new HashMap<String, ConnectorConfig>();
        }
        connectorConfigs.put(type, attribs);
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Project)) {
            return false;
        }
        Project other = (Project) obj;
        return ObjectUtils.equals(id, other.id) && ObjectUtils.equals(state, other.state)
                && ObjectUtils.equals(notificationRecipient, other.notificationRecipient)
                && ObjectUtils.equals(services, other.services);
    }

    @Override
    public int hashCode() {
        int hash = 17;
        hash += 31 * ObjectUtils.hashCode(id);
        hash += 31 * ObjectUtils.hashCode(state);
        hash += 31 * ObjectUtils.hashCode(notificationRecipient);
        hash += 31 * ObjectUtils.hashCode(services);
        return hash;
    }
}

@@ -2906,32 +2906,263 @@
 ttribs);%0A    %7D%0A%0A
+    private boolean objectEquals(Object o1, Object o2) %7B%0A        /* Needed for the persistence service */%0A        if (o1 == null %7C%7C o2 == null) %7B%0A            return true;%0A        %7D%0A        return ObjectUtils.equals(o1, o2);%0A    %7D%0A%0A
     @Override%0A  
@@ -3315,24 +3315,25 @@
 t) obj;%0A
+%0A
         
 return O
@@ -3328,243 +3328,441 @@
     
-return ObjectUtils.equals(id, other.id) && ObjectUtils.equals(state, other.state)%0A                && ObjectUtils.equals(notificationRecipient, other.notificationRecipient)%0A                && ObjectUtils.equals(services, other.services)
+if (!objectEquals(id, other.id)) return false;%0A        if (!objectEquals(notificationRecipient, other.notificationRecipient)) return false;%0A        if (!objectEquals(services, other.services)) return false;%0A        if (!objectEquals(state, other.state)) return false;%0A        if (!objectEquals(connectorConfigs, other.connectorConfigs)) return false;%0A        /* Ignore the last poll date, it will go away soon */%0A%0A        return true
 ;%0A  

Implement an equals method that makes the new DB happy