This project has retired. For details please refer to its Attic page.
Uddiuddiorgcategorizationderivedfrom xref
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.model.Tmodel;
24  import org.apache.juddi.v3.error.ErrorMessage;
25  import org.apache.juddi.v3.error.InvalidValueException;
26  import org.uddi.api_v3.BindingTemplate;
27  import org.uddi.api_v3.BusinessEntity;
28  import org.uddi.api_v3.BusinessService;
29  import org.uddi.api_v3.KeyedReference;
30  import org.uddi.api_v3.PublisherAssertion;
31  import org.uddi.api_v3.TModel;
32  import org.uddi.api_v3.TModelInstanceInfo;
33  import org.uddi.v3_service.DispositionReportFaultMessage;
34  
35  /**
36   * UDDI provides a mechanism that may be used by publishers to categorize UDDI
37   * entities according to any number of category systems. See Appendix F Using
38   * Categorization for more information. This section defines a tModel used to
39   * associate a tModel, frequently a category system, with some other tModel,
40   * frequently the value set of some other category system, for the purpose of
41   * extension or redefinition of purpose.
42   *
43   * Most value sets are used with some purpose in mind. To avoid ambiguity in
44   * publisher and inquirer intent it is not uncommon for this purpose to be
45   * explicitly associated with the value set in its tModel. The IS0 3166
46   * geographic category system, for example, has the purpose service offering
47   * area.
48   *
49   * Similarly, the UDDI API is comprised of a fixed set of programming interfaces
50   * and structures. UDDI registries can extend the UDDI API through schema
51   * derivation, to offer additional functionality.
52   *
53   * The Derived From category system exists to allow tModels to refer to the
54   * tModels that they extend in some way. Value set values can be re-used by
55   * referring a derived value set tModel to the values in some other value set
56   * tModel. The reason for reuse can be for assigning another purpose to the set
57   * of values, for extending the set of values, for associating one set of values
58   * with another, or for some other kind of derivation. * Specification tModels
59   * that extend some other specification tModel can similarly use this category
60   * system to refer to the tModels they extend, providing end users with
61   * knowledge about the full scope of the API.
62   * <br><br>
63   * Valid Values - The keyValue attribute in a keyedReference element that
64   * references this tModel within a categoryBag MUST be some other tModelKey in
65   * the UDDI registry. For value set derivations the tModel that is referred to
66   * contain the root values for the derived value set. A tModel for a derived
67   * value set is not automatically checked if the referred to value set is
68   * checked. The derived value set must itself go through the registry's process
69   * for making the derived value set checked.
70   *
71   * @author Alex O'Ree
72   */
73  public class Uddiuddiorgcategorizationderivedfrom implements ValueSetValidator {
74  
75          public String getMyKey() {
76                  return "uddi:uddi.org:categorization:derivedfrom";
77          }
78  
79          @Override
80          public void validateValuesBindingTemplate(List<BindingTemplate> items, String xpath) throws DispositionReportFaultMessage {
81                  if (items == null) {
82                          return;
83                  }
84  
85                  for (int i = 0; i < items.size(); i++) {
86                          if (items.get(i).getCategoryBag() != null) {
87                                  AbstractSimpleValidator.validateKeyNotPresentKeyRef(items.get(i).getCategoryBag().getKeyedReference(), getMyKey(), "binding");
88                                  AbstractSimpleValidator.validateKeyNotPresentKeyRefGrp(items.get(i).getCategoryBag().getKeyedReferenceGroup(), getMyKey(), "binding");
89                          }
90                          if (items.get(i).getTModelInstanceDetails() != null) {
91                                  for (int k = 0; k < items.get(i).getTModelInstanceDetails().getTModelInstanceInfo().size(); k++) {
92                                          if (items.get(i).getTModelInstanceDetails().getTModelInstanceInfo().get(k) != null) {
93                                                  if (getMyKey().equalsIgnoreCase(items.get(i).getTModelInstanceDetails().getTModelInstanceInfo().get(k).getTModelKey())) {
94                                                          throw new InvalidValueException(new ErrorMessage("errors.valuesetvalidation.invalidcontent", "not allowed on binding templates"));
95                                                  }
96                                          }
97                                  }
98                          }
99                  }
100         }
101 
102         @Override
103         public void validateValuesBusinessEntity(List<BusinessEntity> items) throws DispositionReportFaultMessage {
104                 if (items == null) {
105                         return;
106                 }
107                 for (int i = 0; i < items.size(); i++) {
108                         if (items.get(i).getCategoryBag() != null) {
109                                 AbstractSimpleValidator.validateKeyNotPresentKeyRef(items.get(i).getCategoryBag().getKeyedReference(), getMyKey(), "business");
110                                 AbstractSimpleValidator.validateKeyNotPresentKeyRefGrp(items.get(i).getCategoryBag().getKeyedReferenceGroup(), getMyKey(), "business");
111                         }
112                         if (items.get(i).getIdentifierBag() != null) {
113                                 AbstractSimpleValidator.validateKeyNotPresentKeyRef(items.get(i).getCategoryBag().getKeyedReference(), getMyKey(), "business");
114                         }
115                         if (items.get(i).getBusinessServices() != null) {
116                                 validateValuesBusinessService(items.get(i).getBusinessServices().getBusinessService(), "businessEntity(" + i + ").");
117                         }
118                 }
119         }
120 
121         @Override
122         public void validateValuesBusinessService(List<BusinessService> items, String xpath) throws DispositionReportFaultMessage {
123                 if (items == null) {
124                         return;
125                 }
126                 for (int i = 0; i < items.size(); i++) {
127                         if (items.get(i).getCategoryBag() != null) {
128                                 AbstractSimpleValidator.validateKeyNotPresentKeyRef(items.get(i).getCategoryBag().getKeyedReference(), getMyKey(), "service");
129                                 AbstractSimpleValidator.validateKeyNotPresentKeyRefGrp(items.get(i).getCategoryBag().getKeyedReferenceGroup(), getMyKey(), "service");
130                         }
131                         if (items.get(i).getBindingTemplates() != null) {
132                                 validateValuesBindingTemplate(items.get(i).getBindingTemplates().getBindingTemplate(), xpath + xpath + "businessService(" + i + ").identifierBag.");
133                         }
134                 }
135         }
136 
137         @Override
138         public void validateValuesPublisherAssertion(List<PublisherAssertion> items) throws DispositionReportFaultMessage {
139                 if (items == null) {
140                         return;
141                 }
142                 for (int i = 0; i < items.size(); i++) {
143                         AbstractSimpleValidator.validateKeyNotPresentKeyRef(items.get(i).getKeyedReference(), getMyKey(), "publisherAssertion");
144                 }
145         }
146 
147         private void validatedValuesKeyRef(List<KeyedReference> items, String xpath) throws DispositionReportFaultMessage {
148                 if (items == null) {
149                         return;
150                 }
151                 StringBuilder err = new StringBuilder();
152                 for (int i = 0; i < items.size(); i++) {
153                         List<String> validValues = getValidValues();
154                         if (validValues != null) {
155                                 //ok we have some work to do
156                                 boolean valid = false;
157                                 for (int k = 0; k < validValues.size(); k++) {
158                                         if (validValues.get(k).equals(items.get(i).getKeyValue())) {
159                                                 valid = true;
160                                         }
161                                 }
162                                 if (!valid) {
163                                         err.append(xpath + "keyedReference(" + i + ") ");
164                                 }
165                         }
166                 }
167                 if (err.length() > 0) {
168                         throw new InvalidValueException(new ErrorMessage("errors.valuesetvalidation.invalidcontent", err.toString()));
169                 }
170         }
171 
172         @Override
173         public void validateTmodelInstanceDetails(List<TModelInstanceInfo> tModelInstanceInfo, String xpath) throws DispositionReportFaultMessage {
174                 if (tModelInstanceInfo == null) {
175                         return;
176                 }
177                 for (int k = 0; k < tModelInstanceInfo.size(); k++) {
178                         if (getMyKey().equalsIgnoreCase(tModelInstanceInfo.get(k).getTModelKey())) {
179                                 throw new InvalidValueException(new ErrorMessage("errors.valuesetvalidation.invalidcontent", "not allowed on tModel instance info"));
180                         }
181                 }
182         }
183 
184         @Override
185         public void validateValuesTModel(List<TModel> items) throws DispositionReportFaultMessage {
186                 if (items == null) {
187                         return;
188                 }
189                 EntityManager em = PersistenceManager.getEntityManager();
190                 EntityTransaction tx = em.getTransaction();
191                 try {
192                         for (int i = 0; i < items.size(); i++) {
193                                 if (items.get(i).getCategoryBag() != null) {
194                                         for (int k = 0; k < items.get(i).getCategoryBag().getKeyedReference().size(); k++) {
195                                                 if (getMyKey().equalsIgnoreCase(items.get(i).getCategoryBag().getKeyedReference().get(k).getTModelKey())) {
196                                                         //TODO look up value
197                                                         Tmodel find = em.find(org.apache.juddi.model.Tmodel.class, items.get(i).getCategoryBag().getKeyedReference().get(k).getKeyValue());
198                                                         if (find == null) {
199                                                                 throw new InvalidValueException(new ErrorMessage("errors.valuesetvalidation.invalidcontent", "Referenced key " + items.get(i).getCategoryBag().getKeyedReference().get(k).getKeyValue() + " does not exist"));
200                                                         }
201                                                 }
202                                         }
203                                 }
204                                 if (items.get(i).getIdentifierBag() != null) {
205                                         AbstractSimpleValidator.validateKeyNotPresentKeyRef(items.get(i).getIdentifierBag().getKeyedReference(), getMyKey(), "tmodel identbag");
206                                 }
207                         }
208                 } catch (DispositionReportFaultMessage d) {
209                         throw d;
210                 } finally {
211                         if (tx.isActive()) {
212                                 tx.rollback();
213                         }
214                         em.close();
215                 }
216         }
217 
218         @Override
219         public List<String> getValidValues() {
220                 return Collections.EMPTY_LIST;
221         }
222 
223 }