View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.provisioning.java.data;
20  
21  import org.apache.syncope.common.lib.SyncopeClientCompositeException;
22  import org.apache.syncope.common.lib.SyncopeClientException;
23  import org.apache.syncope.common.lib.to.AttrRepoTO;
24  import org.apache.syncope.common.lib.to.Item;
25  import org.apache.syncope.common.lib.types.ClientExceptionType;
26  import org.apache.syncope.common.lib.types.MappingPurpose;
27  import org.apache.syncope.core.persistence.api.entity.EntityFactory;
28  import org.apache.syncope.core.persistence.api.entity.am.AttrRepo;
29  import org.apache.syncope.core.provisioning.api.data.AttrRepoDataBinder;
30  import org.apache.syncope.core.provisioning.api.jexl.JexlUtils;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  public class AttrRepoDataBinderImpl implements AttrRepoDataBinder {
35  
36      protected static final Logger LOG = LoggerFactory.getLogger(AttrRepoDataBinder.class);
37  
38      protected final EntityFactory entityFactory;
39  
40      public AttrRepoDataBinderImpl(final EntityFactory entityFactory) {
41          this.entityFactory = entityFactory;
42      }
43  
44      protected void populateItems(final AttrRepoTO attrRepoTO, final AttrRepo attrRepo) {
45          SyncopeClientCompositeException scce = SyncopeClientException.buildComposite();
46          SyncopeClientException invalidMapping =
47                  SyncopeClientException.build(ClientExceptionType.InvalidMapping);
48          SyncopeClientException requiredValuesMissing =
49                  SyncopeClientException.build(ClientExceptionType.RequiredValuesMissing);
50  
51          attrRepoTO.getItems().forEach(itemTO -> {
52              if (itemTO == null) {
53                  LOG.error("Null {}", Item.class.getSimpleName());
54                  invalidMapping.getElements().add("Null " + Item.class.getSimpleName());
55              } else if (itemTO.getIntAttrName() == null) {
56                  requiredValuesMissing.getElements().add("intAttrName");
57                  scce.addException(requiredValuesMissing);
58              } else {
59                  // no mandatory condition implies mandatory condition false
60                  if (!JexlUtils.isExpressionValid(itemTO.getMandatoryCondition() == null
61                          ? "false" : itemTO.getMandatoryCondition())) {
62  
63                      SyncopeClientException invalidMandatoryCondition =
64                              SyncopeClientException.build(ClientExceptionType.InvalidValues);
65                      invalidMandatoryCondition.getElements().add(itemTO.getMandatoryCondition());
66                      scce.addException(invalidMandatoryCondition);
67                  }
68  
69                  Item item = new Item();
70                  item.setIntAttrName(itemTO.getIntAttrName());
71                  item.setExtAttrName(itemTO.getExtAttrName());
72                  item.setMandatoryCondition(itemTO.getMandatoryCondition());
73                  item.setConnObjectKey(itemTO.isConnObjectKey());
74                  item.setPassword(itemTO.isPassword());
75                  item.setPropagationJEXLTransformer(itemTO.getPropagationJEXLTransformer());
76                  item.setPullJEXLTransformer(itemTO.getPullJEXLTransformer());
77                  attrRepo.getItems().add(item);
78              }
79          });
80  
81          if (!invalidMapping.getElements().isEmpty()) {
82              scce.addException(invalidMapping);
83          }
84          if (scce.hasExceptions()) {
85              throw scce;
86          }
87      }
88  
89      @Override
90      public AttrRepo create(final AttrRepoTO attrRepoTO) {
91          AttrRepo attrRepo = entityFactory.newEntity(AttrRepo.class);
92          attrRepo.setKey(attrRepoTO.getKey());
93          return update(attrRepo, attrRepoTO);
94      }
95  
96      @Override
97      public AttrRepo update(final AttrRepo attrRepo, final AttrRepoTO attrRepoTO) {
98          attrRepo.setDescription(attrRepoTO.getDescription());
99          attrRepo.setState(attrRepoTO.getState());
100         attrRepo.setOrder(attrRepoTO.getOrder());
101         attrRepo.setConf(attrRepoTO.getConf());
102 
103         attrRepo.getItems().clear();
104         populateItems(attrRepoTO, attrRepo);
105 
106         return attrRepo;
107     }
108 
109     protected void populateItems(final AttrRepo attrRepo, final AttrRepoTO attrRepoTO) {
110         attrRepo.getItems().forEach(item -> {
111             Item itemTO = new Item();
112             itemTO.setIntAttrName(item.getIntAttrName());
113             itemTO.setExtAttrName(item.getExtAttrName());
114             itemTO.setMandatoryCondition(item.getMandatoryCondition());
115             itemTO.setConnObjectKey(item.isConnObjectKey());
116             itemTO.setPassword(item.isPassword());
117             itemTO.setPropagationJEXLTransformer(item.getPropagationJEXLTransformer());
118             itemTO.setPullJEXLTransformer(item.getPullJEXLTransformer());
119             itemTO.setPurpose(MappingPurpose.NONE);
120 
121             attrRepoTO.getItems().add(itemTO);
122         });
123     }
124 
125     @Override
126     public AttrRepoTO getAttrRepoTO(final AttrRepo attrRepo) {
127         AttrRepoTO attrRepoTO = new AttrRepoTO();
128 
129         attrRepoTO.setKey(attrRepo.getKey());
130         attrRepoTO.setDescription(attrRepo.getDescription());
131         attrRepoTO.setState(attrRepo.getState());
132         attrRepoTO.setOrder(attrRepo.getOrder());
133         attrRepoTO.setConf(attrRepo.getConf());
134 
135         populateItems(attrRepo, attrRepoTO);
136 
137         return attrRepoTO;
138     }
139 }