/*******************************************************************************
 * 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 reciter.xml.retriever.pubmed;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Component;

import reciter.model.identity.AuthorName;
import reciter.model.identity.Identity;
import reciter.pubmed.retriever.PubMedQuery;
import reciter.xml.retriever.pubmed.PubMedQueryType.PubMedQueryBuilder;

@Component("departmentRetrievalStrategy")
public class DepartmentRetrievalStrategy extends AbstractNameRetrievalStrategy {

	private static final String retrievalStrategyName = "DepartmentRetrievalStrategy";
	
	@Override
	public String getRetrievalStrategyName() {
		return retrievalStrategyName;
	}

	@Override
	protected String getStrategySpecificKeyword(Identity identity) {
		if (identity.getOrganizationalUnits() != null && !identity.getOrganizationalUnits().isEmpty()) {
			return identity.getOrganizationalUnits().get(0).getOrganizationalUnitLabel();
		} else {
			return null;
		}
	}

	@Override
	protected PubMedQuery buildNameQuery(Set<AuthorName> identitynames, Identity identity) {
		PubMedQueryBuilder pubMedQueryBuilder = 
				new PubMedQueryBuilder(getStrategySpecificKeyword(identity))
					.author(true, false, identitynames);
		
		return pubMedQueryBuilder.build();
	}
	
	@Override
	protected PubMedQuery buildNameQuery(Set<AuthorName> identitynames, Identity identity, Date startDate,
			Date endDate) {
		PubMedQueryBuilder pubMedQueryBuilder = 
				new PubMedQueryBuilder(getStrategySpecificKeyword(identity))
					.author(true, false, identitynames)
					.dateRange(true, startDate, endDate);
		
		return pubMedQueryBuilder.build();
	}
}

@@ -1043,24 +1043,51 @@
 .util.Date;%0A
+import java.util.Iterator;%0A
 import java.
@@ -1097,16 +1097,16 @@
 l.List;%0A
-
 import j
@@ -1252,16 +1252,66 @@
 entity;%0A
+import reciter.model.identity.OrganizationalUnit;%0A
 import r
@@ -1904,82 +1904,650 @@
 %0A%09%09%09
-return identity.getOrganizationalUnits().get(0).getOrganizationalUnitLabel
+Iterator%3COrganizationalUnit%3E iter = identity.getOrganizationalUnits().iterator();%0A%09%09%09final OrganizationalUnit firstDepartment = iter.next(); %0A%09%09%09final String first%09= firstDepartment.getOrganizationalUnitLabel() + %22%5Baffiliation%5D%22;%0A%09%09%09if (!iter.hasNext()) %7B%0A%09%09%09%09return first;%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09//for more than 1 institutions %0A%09%09%09final StringBuilder buf = new StringBuilder();%0A%09%09%09if(first != null) %7B%0A%09%09%09%09buf.append(%22(%22 + first);%0A%09%09%09%7D %0A%09%09%09while(iter.hasNext()) %7B%0A%09%09%09%09OrganizationalUnit orgUnit = iter.next();%0A%09%09%09%09buf.append(%22 OR %22);%0A%09%09%09%09buf.append(orgUnit.getOrganizationalUnitLabel() + %22%5Baffiliation%5D%22);%0A%09%09%09%7D%0A%09%09%09buf.append(%22)%22);%0A%09%09%09return buf.toString
 ();%0A

change to include all departments in retrieval