View Javadoc
1   /*
2    * Copyright 2001-2008 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   */
17  package org.apache.juddi.validation;
18  
19  import java.util.List;
20  import java.util.logging.Level;
21  import java.util.logging.Logger;
22  import javax.persistence.EntityManager;
23  import javax.persistence.EntityTransaction;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.juddi.api_v3.ValidValues;
27  import org.apache.juddi.config.PersistenceManager;
28  import org.apache.juddi.mapping.MappingModelToApi;
29  import org.apache.juddi.model.Tmodel;
30  import org.apache.juddi.model.UddiEntityPublisher;
31  import org.apache.juddi.v3.error.ErrorMessage;
32  import org.apache.juddi.v3.error.ValueNotAllowedException;
33  import org.uddi.api_v3.TModel;
34  import org.uddi.v3_service.DispositionReportFaultMessage;
35   
36  /**
37   * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>
38   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
39   */
40  public class ValidateValueSetValidation extends ValidateUDDIApi {
41  
42          private static Log log = LogFactory.getLog(ValidateValueSetValidation.class);
43  
44          public ValidateValueSetValidation(UddiEntityPublisher publisher) {
45                  super(publisher);
46          }
47  
48          /**
49           * called from jUDDI API SetAllValidValues
50           *
51           * @param values
52           */
53          public void validateSetAllValidValues(List<ValidValues> values) throws ValueNotAllowedException {
54                  if (values == null || values.isEmpty()) {
55                          throw new ValueNotAllowedException(new ErrorMessage("errors.NullInput"));
56                  }
57  
58                  for (int i = 0; i < values.size(); i++) {
59                          String key = values.get(i).getTModekKey();
60                          if (key == null || key.trim().length() == 0) {
61                                  throw new ValueNotAllowedException(new ErrorMessage("errors.NullInput", "tModel key"));
62                          }
63                          //ensure tmodel exists
64                          Tmodel tm = GetTModel_MODEL_IfExists(values.get(i).getTModekKey());
65                          
66                          if (tm == null) {
67                                  throw new ValueNotAllowedException(new ErrorMessage("errors.invalidkey.TModelNotFound", key));
68                          }
69                          //ensure caller owns the tModel
70                          if (!this.publisher.isOwner(tm)) {
71                                  throw new ValueNotAllowedException(new ErrorMessage("errors.usermismatch.InvalidOwner", key ));
72                          }
73  
74                          //if we have no values, it may be to simply unset any values
75  
76                          /*//validate that we have values
77                           if (values.get(i).getValue() == null || values.get(i).getValue().isEmpty()) {
78                           throw new ValueNotAllowedException(new ErrorMessage("errors.NullInput", "value[]"));
79                           }
80                           //and that they aren't empty
81                           for (int k = 0; k < values.get(i).getValue().size(); k++) {
82                           if (values.get(i).getValue().get(k) == null || values.get(i).getValue().get(k).trim().length() == 0) {
83                           throw new ValueNotAllowedException(new ErrorMessage("errors.NullInput", "value[" + i + "].value"));
84                           }
85                           }*/
86                  }
87          }
88  
89          /**
90           * return the publisher
91           *
92           * @param tmodelKey
93           * @return
94           * @throws ValueNotAllowedException
95           */
96          public static TModel GetTModel_API_IfExists(String tmodelKey) throws ValueNotAllowedException {
97                  EntityManager em = PersistenceManager.getEntityManager();
98  
99                  TModel apitmodel = null;
100                 if (em == null) {
101                         //this is normally the Install class firing up
102                         log.warn(new ErrorMessage("errors.tmodel.ReferentialIntegrityNullEM"));
103                         return null;
104                 } else {
105 
106 
107                         EntityTransaction tx = em.getTransaction();
108                         try {
109                                 Tmodel modelTModel = null;
110                                 tx.begin();
111                                 modelTModel = em.find(org.apache.juddi.model.Tmodel.class, tmodelKey);
112                                 if (modelTModel != null) {
113                                         apitmodel = new TModel();
114                                         try {
115                                                 MappingModelToApi.mapTModel(modelTModel, apitmodel);
116                                         } catch (DispositionReportFaultMessage ex) {
117                                                 log.warn(ex);
118                                                 apitmodel=null;
119                                         }
120 
121 
122                                 }
123                                 tx.commit();
124                         } finally {
125                                 if (tx.isActive()) {
126                                         tx.rollback();
127                                 }
128                                 em.close();
129                         }
130 
131                 }
132                 return apitmodel;
133         }
134         
135         
136         public static Tmodel GetTModel_MODEL_IfExists(String tmodelKey) throws ValueNotAllowedException {
137                 EntityManager em = PersistenceManager.getEntityManager();
138 
139                 Tmodel model = null;
140                 if (em == null) {
141                         //this is normally the Install class firing up
142                         log.warn(new ErrorMessage("errors.tmodel.ReferentialIntegrityNullEM"));
143                         return null;
144                 } else {
145 
146 
147                         EntityTransaction tx = em.getTransaction();
148                         try {
149                                 
150                                 tx.begin();
151                                 model = em.find(org.apache.juddi.model.Tmodel.class, tmodelKey);
152                                 tx.commit();
153                         } finally {
154                                 if (tx.isActive()) {
155                                         tx.rollback();
156                                 }
157                                 em.close();
158                         }
159 
160                 }
161                 return model;
162         }
163 }