View Javadoc
1   package ca.uhn.fhir.jpa.sp;
2   
3   /*-
4    * #%L
5    * HAPI FHIR JPA Server
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import ca.uhn.fhir.jpa.dao.DaoConfig;
24  import ca.uhn.fhir.jpa.dao.data.ISearchParamPresentDao;
25  import ca.uhn.fhir.jpa.entity.ResourceTable;
26  import ca.uhn.fhir.jpa.entity.SearchParamPresent;
27  import org.springframework.beans.factory.annotation.Autowired;
28  
29  import java.util.*;
30  import java.util.Map.Entry;
31  
32  public class SearchParamPresenceSvcImpl implements ISearchParamPresenceSvc {
33  
34  	@Autowired
35  	private ISearchParamPresentDao mySearchParamPresentDao;
36  
37  	@Autowired
38  	private DaoConfig myDaoConfig;
39  
40  	@Override
41  	public void updatePresence(ResourceTable theResource, Map<String, Boolean> theParamNameToPresence) {
42  		if (myDaoConfig.getIndexMissingFields() == DaoConfig.IndexEnabledEnum.DISABLED) {
43  			return;
44  		}
45  
46  		Map<String, Boolean> presenceMap = new HashMap<>(theParamNameToPresence);
47  
48  		// Find existing entries
49  		Collection<SearchParamPresent> existing;
50  		existing = mySearchParamPresentDao.findAllForResource(theResource);
51  		Map<Long, SearchParamPresent> existingHashToPresence = new HashMap<>();
52  		for (SearchParamPresent nextExistingEntity : existing) {
53  			existingHashToPresence.put(nextExistingEntity.getHashPresence(), nextExistingEntity);
54  		}
55  
56  		// Find newly wanted set of entries
57  		Map<Long, SearchParamPresent> newHashToPresence = new HashMap<>();
58  		for (Entry<String, Boolean> next : presenceMap.entrySet()) {
59  			String paramName = next.getKey();
60  
61  			SearchParamPresent present = new SearchParamPresent();
62  			present.setResource(theResource);
63  			present.setParamName(paramName);
64  			present.setPresent(next.getValue());
65  			present.calculateHashes();
66  
67  			newHashToPresence.put(present.getHashPresence(), present);
68  		}
69  
70  		// Delete any that should be deleted
71  		List<SearchParamPresent> toDelete = new ArrayList<>();
72  		for (Entry<Long, SearchParamPresent> nextEntry : existingHashToPresence.entrySet()) {
73  			if (newHashToPresence.containsKey(nextEntry.getKey()) == false) {
74  				toDelete.add(nextEntry.getValue());
75  			}
76  		}
77  		mySearchParamPresentDao.deleteInBatch(toDelete);
78  
79  		// Add any that should be added
80  		List<SearchParamPresent> toAdd = new ArrayList<>();
81  		for (Entry<Long, SearchParamPresent> nextEntry : newHashToPresence.entrySet()) {
82  			if (existingHashToPresence.containsKey(nextEntry.getKey()) == false) {
83  				toAdd.add(nextEntry.getValue());
84  			}
85  		}
86  		mySearchParamPresentDao.saveAll(toAdd);
87  
88  	}
89  
90  }