/**
 * Licensed to Jasig under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Jasig 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.openregistry.core.service;

import java.util.List;

import org.openregistry.core.domain.DisclosureSettings;
import org.openregistry.core.domain.sor.SorPerson;

/**
 * @author lidial 
 * @version $Revision$ $Date$
 * @since 1.0.0
 */
public class DefaultDisclosureSettingsFieldElector implements FieldElector<DisclosureSettings> {

	/**
	 * Default implementation simply returns the newest value
	 * @see org.openregistry.core.service.FieldElector#elect(org.openregistry.core.domain.sor.SorPerson, java.util.List, boolean)
	 */
	public DisclosureSettings elect(SorPerson newestPerson,
			List<SorPerson> sorPersons, boolean deletion) {
		
		if (newestPerson != null && newestPerson.getDisclosureSettings() != null) {
			return newestPerson.getDisclosureSettings();
		} else if (sorPersons != null && !sorPersons.isEmpty()) {
				return sorPersons.get(0).getDisclosureSettings();
		} else {
			return null;
		}
	}
}

@@ -888,16 +888,23 @@
 .domain.
+sor.Sor
 Disclosu
@@ -1123,16 +1123,19 @@
 Elector%3C
+Sor
 Disclosu
@@ -1210,17 +1210,130 @@
  new
-es
+ value%0D%0A%09 * If it is no
 t 
+a
 va
-lue
+ilable, returns the most recent of the available ones%0D%0A%09 * If none are available, returns null
 %0D%0A%09 
@@ -1472,16 +1472,19 @@
 %09public 
+Sor
 Disclosu
@@ -1777,57 +1777,560 @@
 %0A%09%09%09
-%09return sorPersons.get(0).getDisclosureSettings()
+SorDisclosureSettings mostRecent = null;%0D%0A%09%09%09%0D%0A%09%09%09for (SorPerson sp: sorPersons) %7B%0D%0A%09%09%09%09if (mostRecent == null %7C%7C %0D%0A%09%09%09%09%09(mostRecent.getLastUpdateDate() != null  %0D%0A%09%09%09%09%09%09&& sp.getDisclosureSettings() != null && sp.getDisclosureSettings().getLastUpdateDate() != null%0D%0A%09%09%09%09%09%09&& !sp.getDisclosureSettings().getLastUpdateDate().before(mostRecent.getLastUpdateDate()))) %7B%0D%0A%09%09%09%09%09mostRecent = (sp.getDisclosureSettings() != null && sp.getDisclosureSettings().getLastUpdateDate() != null) ? sp.getDisclosureSettings() : null;%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D%0D%0A%09%09%09return mostRecent
 ;%0D%0A%09

Return the most recent value if the new one is not available