View Javadoc
1   /*
2    * Copyright 2014 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.juddi.validation.vsv;
17  
18  import java.util.Collections;
19  import java.util.List;
20  import javax.persistence.EntityManager;
21  import javax.persistence.EntityTransaction;
22  import org.apache.juddi.config.PersistenceManager;
23  import org.apache.juddi.v3.error.ErrorMessage;
24  import org.apache.juddi.v3.error.InvalidValueException;
25  import org.uddi.api_v3.BindingTemplate;
26  import org.uddi.api_v3.BusinessEntity;
27  import org.uddi.api_v3.BusinessService;
28  import org.uddi.api_v3.PublisherAssertion;
29  import org.uddi.api_v3.TModel;
30  import org.uddi.api_v3.TModelInstanceInfo;
31  import org.uddi.v3_service.DispositionReportFaultMessage;
32  
33  /**
34   * This tModel represents a category system that is used to point a tModel
35   * representing a checked value set to the bindingTemplate for a value set
36   * caching or value set validation Web service.
37   *
38   * One of the concepts that tModels can represent is a checked value set. A
39   * checked value set is one whose use is monitored by a validation algorithm.
40   * There are two types of validation algorithms: simple checking of referenced
41   * values against a pre-defined set of allowable values, and any other kind of
42   * validation. UDDI provides the Value Set API set (see Section 5.6 Value Set
43   * API Set) to acquire the set of allowable values or execute an external
44   * validation algorithm.
45   *
46   * A validation algorithm for a checked value set can be acquired by nodes
47   * privately, or can be obtained through normal UDDI discovery. The validatedBy
48   * category system facilitates discovery of the value set caching or value set
49   * validation Web service for a checked value set tModel by pointing to the
50   * bindingTemplate for the Web service.  *
51   * For the Web service to be useful, it must recognize any and all checked value
52   * sets that it is expected to be associated with. The recommended way for doing
53   * so is to place the tModels for the checked value sets it supports in the
54   * tModelInstanceDetails of the bindingTemplate for the Web service. Registry
55   * policy may require that providers of the Web service recognize value sets
56   * supported using this technique.
57   * 
58  * Valid Values
59   *
60   * The keyValues in keyedReferences that refer to this tModel must be
61   * bindingKeys. Such a keyValue SHOULD reference a bindingTemplate that
62   * specifies a Web service that implements a value set caching or value set
63   * validation API and which SHOULD reference the value set tModel so categorized
64   * with this category system. No other contextual checks are performed.
65   *
66   * @author Alex O'Ree
67   */
68  public class Uddiuddiorgcategorizationvalidatedby implements ValueSetValidator{
69  
70           
71          public String getKey(){
72                  return "uddi:uddi.org:categorization:validatedby";
73          }
74          @Override
75          public void validateValuesBindingTemplate(List<BindingTemplate> items, String xpath) throws DispositionReportFaultMessage {
76                  if (items == null) {
77                          return;
78                  }
79  
80                  for (int i = 0; i < items.size(); i++) {
81                          if (items.get(i).getCategoryBag() != null) {
82                                  AbstractSimpleValidator.validateKeyNotPresentKeyRef(items.get(i).getCategoryBag().getKeyedReference(), getKey(), "binding");
83                                  AbstractSimpleValidator.validateKeyNotPresentKeyRefGrp(items.get(i).getCategoryBag().getKeyedReferenceGroup(), getKey(), "binding");
84                          }
85                          if (items.get(i).getTModelInstanceDetails() != null) {
86                                  for (int k = 0; k < items.get(i).getTModelInstanceDetails().getTModelInstanceInfo().size(); k++) {
87                                          if (items.get(i).getTModelInstanceDetails().getTModelInstanceInfo().get(k) != null) 
88                                          if (getKey().equalsIgnoreCase(items.get(i).getTModelInstanceDetails().getTModelInstanceInfo().get(k).getTModelKey())) {
89                                                  throw new InvalidValueException(new ErrorMessage("errors.valuesetvalidation.invalidcontent", "not allowed on binding templates"));
90                                          }
91                                  }
92                          }
93                  }
94          }
95  
96          @Override
97          public void validateValuesBusinessEntity(List<BusinessEntity> items) throws DispositionReportFaultMessage {
98                  if (items == null) {
99                          return;
100                 }
101                 for (int i = 0; i < items.size(); i++) {
102                         if (items.get(i).getCategoryBag() != null) {
103                                 AbstractSimpleValidator.validateKeyNotPresentKeyRef(items.get(i).getCategoryBag().getKeyedReference(), getKey(), "business");
104                                 AbstractSimpleValidator.validateKeyNotPresentKeyRefGrp(items.get(i).getCategoryBag().getKeyedReferenceGroup(), getKey(), "business");
105                         }
106                         if (items.get(i).getIdentifierBag() != null) {
107                                 AbstractSimpleValidator.validateKeyNotPresentKeyRef(items.get(i).getCategoryBag().getKeyedReference(), getKey(), "business");
108                         }
109                         if (items.get(i).getBusinessServices() != null) {
110                                 validateValuesBusinessService(items.get(i).getBusinessServices().getBusinessService(), "businessEntity(" + i + ").");
111                         }
112                 }
113         }
114 
115         @Override
116         public void validateValuesBusinessService(List<BusinessService> items, String xpath) throws DispositionReportFaultMessage {
117                 if (items == null) {
118                         return;
119                 }
120                 for (int i = 0; i < items.size(); i++) {
121                         if (items.get(i).getCategoryBag() != null) {
122                                 AbstractSimpleValidator.validateKeyNotPresentKeyRef(items.get(i).getCategoryBag().getKeyedReference(), getKey(), "service");
123                                 AbstractSimpleValidator.validateKeyNotPresentKeyRefGrp(items.get(i).getCategoryBag().getKeyedReferenceGroup(), getKey(), "service");
124                         }
125                         if (items.get(i).getBindingTemplates() != null) {
126                                 validateValuesBindingTemplate(items.get(i).getBindingTemplates().getBindingTemplate(), xpath + xpath + "businessService(" + i + ").identifierBag.");
127                         }
128                 }
129         }
130 
131         @Override
132         public void validateValuesPublisherAssertion(List<PublisherAssertion> items) throws DispositionReportFaultMessage {
133                 if (items == null) {
134                         return;
135                 }
136                 for (int i = 0; i < items.size(); i++) {
137                         AbstractSimpleValidator.validateKeyNotPresentKeyRef(items.get(i).getKeyedReference(), getKey(), "publisherAssertion");
138                 }
139         }
140 
141       
142         @Override
143         public void validateTmodelInstanceDetails(List<TModelInstanceInfo> tModelInstanceInfo, String xpath) throws DispositionReportFaultMessage {
144                 if (tModelInstanceInfo == null) {
145                         return;
146                 }
147                 for (int k = 0; k < tModelInstanceInfo.size(); k++) {
148                         if (getKey().equalsIgnoreCase(tModelInstanceInfo.get(k).getTModelKey())) {
149                                 throw new InvalidValueException(new ErrorMessage("errors.valuesetvalidation.invalidcontent", "not allowed on tModel instance info"));
150                         }
151                 }
152         }
153 
154         @Override
155         public void validateValuesTModel(List<TModel> items) throws DispositionReportFaultMessage {
156                 if (items == null) {
157                         return;
158                 }
159                 EntityManager em = PersistenceManager.getEntityManager();
160                 EntityTransaction tx = em.getTransaction();
161                 try {
162                         for (int i = 0; i < items.size(); i++) {
163                                 if (items.get(i).getCategoryBag() != null) {
164                                         for (int k = 0; k < items.get(i).getCategoryBag().getKeyedReference().size(); k++) {
165                                                 if (getKey().equalsIgnoreCase(items.get(i).getCategoryBag().getKeyedReference().get(k).getTModelKey())) {
166                                                        
167                                                         org.apache.juddi.model.BindingTemplate find = em.find(org.apache.juddi.model.BindingTemplate.class, items.get(i).getCategoryBag().getKeyedReference().get(k).getKeyValue());
168                                                         if (find == null) {
169                                                                 throw new InvalidValueException(new ErrorMessage("errors.valuesetvalidation.invalidcontent", "Referenced key " + items.get(i).getCategoryBag().getKeyedReference().get(k).getKeyValue() + " does not exist"));
170                                                         }
171                                                 }
172                                         }
173                                 }
174                                 if (items.get(i).getIdentifierBag() != null) {
175                                         AbstractSimpleValidator.validateKeyNotPresentKeyRef(items.get(i).getIdentifierBag().getKeyedReference(), getKey(), "tmodel identbag");
176                                 }
177                         }
178                 } catch (DispositionReportFaultMessage d) {
179                         throw d;
180                 } finally {
181                         if (tx.isActive()) {
182                                 tx.rollback();
183                         }
184                         em.close();
185                 }
186         }
187 
188         @Override
189         public List<String> getValidValues() {
190                 return Collections.EMPTY_LIST;
191         }
192 
193 }