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 java.util.Iterator;
22  import java.util.stream.Collectors;
23  import org.apache.syncope.common.lib.SyncopeClientException;
24  import org.apache.syncope.common.lib.to.ApplicationTO;
25  import org.apache.syncope.common.lib.to.PrivilegeTO;
26  import org.apache.syncope.common.lib.types.ClientExceptionType;
27  import org.apache.syncope.core.persistence.api.dao.ApplicationDAO;
28  import org.apache.syncope.core.persistence.api.entity.Application;
29  import org.apache.syncope.core.persistence.api.entity.EntityFactory;
30  import org.apache.syncope.core.persistence.api.entity.Privilege;
31  import org.apache.syncope.core.provisioning.api.data.ApplicationDataBinder;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  public class ApplicationDataBinderImpl implements ApplicationDataBinder {
36  
37      protected static final Logger LOG = LoggerFactory.getLogger(ApplicationDataBinder.class);
38  
39      protected final ApplicationDAO applicationDAO;
40  
41      protected final EntityFactory entityFactory;
42  
43      public ApplicationDataBinderImpl(final ApplicationDAO applicationDAO, final EntityFactory entityFactory) {
44          this.applicationDAO = applicationDAO;
45          this.entityFactory = entityFactory;
46      }
47  
48      @Override
49      public Application create(final ApplicationTO applicationTO) {
50          return update(entityFactory.newEntity(Application.class), applicationTO);
51      }
52  
53      @Override
54      public Application update(final Application toBeUpdated, final ApplicationTO applicationTO) {
55          toBeUpdated.setKey(applicationTO.getKey());
56          Application application = applicationDAO.save(toBeUpdated);
57  
58          application.setDescription(applicationTO.getDescription());
59  
60          // 1. add or update all (valid) privileges from TO
61          applicationTO.getPrivileges().forEach(privilegeTO -> {
62              if (privilegeTO == null) {
63                  LOG.error("Null {}", PrivilegeTO.class.getSimpleName());
64              } else {
65                  Privilege privilege = applicationDAO.findPrivilege(privilegeTO.getKey());
66                  if (privilege == null) {
67                      privilege = entityFactory.newEntity(Privilege.class);
68                      privilege.setKey(privilegeTO.getKey());
69                      privilege.setApplication(application);
70  
71                      application.add(privilege);
72                  } else if (!application.equals(privilege.getApplication())) {
73                      SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidPrivilege);
74                      sce.getElements().add(
75                              "Privilege " + privilege.getKey() + " already owned by " + privilege.getApplication());
76                      throw sce;
77                  }
78  
79                  privilege.setDescription(privilegeTO.getDescription());
80                  privilege.setSpec(privilegeTO.getSpec());
81              }
82          });
83  
84          // 2. remove all privileges not contained in the TO
85          for (Iterator<? extends Privilege> itor = application.getPrivileges().iterator(); itor.hasNext();) {
86              Privilege privilege = itor.next();
87              if (!applicationTO.getPrivileges().stream().
88                      anyMatch(privilegeTO -> privilege.getKey().equals(privilegeTO.getKey()))) {
89  
90                  privilege.setApplication(null);
91                  itor.remove();
92              }
93          }
94  
95          return application;
96      }
97  
98      @Override
99      public PrivilegeTO getPrivilegeTO(final Privilege privilege) {
100         PrivilegeTO privilegeTO = new PrivilegeTO();
101         privilegeTO.setKey(privilege.getKey());
102         privilegeTO.setDescription(privilege.getDescription());
103         privilegeTO.setApplication(privilege.getApplication().getKey());
104         privilegeTO.setSpec(privilege.getSpec());
105         return privilegeTO;
106     }
107 
108     @Override
109     public ApplicationTO getApplicationTO(final Application application) {
110         ApplicationTO applicationTO = new ApplicationTO();
111 
112         applicationTO.setKey(application.getKey());
113         applicationTO.setDescription(application.getDescription());
114         applicationTO.getPrivileges().addAll(
115                 application.getPrivileges().stream().map(this::getPrivilegeTO).collect(Collectors.toList()));
116 
117         return applicationTO;
118     }
119 }