View Javadoc
1   package ca.uhn.fhir.jpa.dao;
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.context.RuntimeResourceDefinition;
24  import ca.uhn.fhir.jpa.dao.data.ISubscriptionTableDao;
25  import ca.uhn.fhir.jpa.entity.ResourceTable;
26  import ca.uhn.fhir.jpa.entity.SubscriptionTable;
27  import ca.uhn.fhir.model.dstu2.resource.Subscription;
28  import ca.uhn.fhir.model.dstu2.valueset.SubscriptionStatusEnum;
29  import ca.uhn.fhir.parser.DataFormatException;
30  import ca.uhn.fhir.rest.api.server.RequestDetails;
31  import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException;
32  import org.hl7.fhir.instance.model.api.IBaseResource;
33  import org.hl7.fhir.instance.model.api.IIdType;
34  import org.springframework.beans.factory.annotation.Autowired;
35  import org.springframework.transaction.PlatformTransactionManager;
36  
37  import java.util.Date;
38  
39  import static org.apache.commons.lang3.StringUtils.isBlank;
40  
41  public class FhirResourceDaoSubscriptionDstu2 extends FhirResourceDaoDstu2<Subscription> implements IFhirResourceDaoSubscription<Subscription> {
42  
43  	@Autowired
44  	private ISubscriptionTableDao mySubscriptionTableDao;
45  
46  	@Autowired
47  	private PlatformTransactionManager myTxManager;
48  
49  	private void createSubscriptionTable(ResourceTable theEntity, Subscription theSubscription) {
50  		SubscriptionTable subscriptionEntity = new SubscriptionTable();
51  		subscriptionEntity.setCreated(new Date());
52  		subscriptionEntity.setSubscriptionResource(theEntity);
53  		myEntityManager.persist(subscriptionEntity);
54  	}
55  
56  	@Override
57  	public Long getSubscriptionTablePidForSubscriptionResource(IIdType theId) {
58  		ResourceTable entity = readEntityLatestVersion(theId);
59  		SubscriptionTable table = mySubscriptionTableDao.findOneByResourcePid(entity.getId());
60  		if (table == null) {
61  			return null;
62  		}
63  		return table.getId();
64  	}
65  
66  
67  	@Override
68  	protected void postPersist(ResourceTable theEntity, Subscription theSubscription) {
69  		super.postPersist(theEntity, theSubscription);
70  
71  		createSubscriptionTable(theEntity, theSubscription);
72  	}
73  
74  
75  	@Override
76  	protected ResourceTable updateEntity(RequestDetails theRequest, IBaseResource theResource, ResourceTable theEntity, Date theDeletedTimestampOrNull, boolean thePerformIndexing, boolean theUpdateVersion,
77  													 Date theUpdateTime, boolean theForceUpdate, boolean theCreateNewHistoryEntry) {
78  		ResourceTable retVal = super.updateEntity(theRequest, theResource, theEntity, theDeletedTimestampOrNull, thePerformIndexing, theUpdateVersion, theUpdateTime, theForceUpdate, theCreateNewHistoryEntry);
79  
80  		if (theDeletedTimestampOrNull != null) {
81  			mySubscriptionTableDao.deleteAllForSubscription(theEntity);
82  		}
83  		return retVal;
84  	}
85  
86  	public RuntimeResourceDefinition validateCriteriaAndReturnResourceDefinition(Subscription theResource) {
87  		String query = theResource.getCriteria();
88  		if (isBlank(query)) {
89  			throw new UnprocessableEntityException("Subscription.criteria must be populated");
90  		}
91  
92  		int sep = query.indexOf('?');
93  		if (sep <= 1) {
94  			throw new UnprocessableEntityException("Subscription.criteria must be in the form \"{Resource Type}?[params]\"");
95  		}
96  
97  		String resType = query.substring(0, sep);
98  		if (resType.contains("/")) {
99  			throw new UnprocessableEntityException("Subscription.criteria must be in the form \"{Resource Type}?[params]\"");
100 		}
101 
102 		RuntimeResourceDefinition resDef;
103 		try {
104 			resDef = getContext().getResourceDefinition(resType);
105 		} catch (DataFormatException e) {
106 			throw new UnprocessableEntityException("Subscription.criteria contains invalid/unsupported resource type: " + resType);
107 		}
108 		return resDef;
109 	}
110 
111 	@Override
112 	protected void validateResourceForStorage(Subscription theResource, ResourceTable theEntityToSave) {
113 		super.validateResourceForStorage(theResource, theEntityToSave);
114 
115 		RuntimeResourceDefinition resDef = validateCriteriaAndReturnResourceDefinition(theResource);
116 
117 		IFhirResourceDao<? extends IBaseResource> dao = getDao(resDef.getImplementingClass());
118 		if (dao == null) {
119 			throw new UnprocessableEntityException("Subscription.criteria contains invalid/unsupported resource type: " + resDef);
120 		}
121 
122 		if (theResource.getChannel().getType() == null) {
123 			throw new UnprocessableEntityException("Subscription.channel.type must be populated on this server");
124 		}
125 
126 		SubscriptionStatusEnum status = theResource.getStatusElement().getValueAsEnum();
127 		if (status == null) {
128 			throw new UnprocessableEntityException("Subscription.status must be populated on this server");
129 		}
130 
131 	}
132 
133 }