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.SyncopeClientException;
22  import org.apache.syncope.common.lib.to.FIQLQueryTO;
23  import org.apache.syncope.common.lib.types.ClientExceptionType;
24  import org.apache.syncope.core.persistence.api.dao.UserDAO;
25  import org.apache.syncope.core.persistence.api.dao.search.SearchCond;
26  import org.apache.syncope.core.persistence.api.entity.EntityFactory;
27  import org.apache.syncope.core.persistence.api.entity.FIQLQuery;
28  import org.apache.syncope.core.persistence.api.search.SearchCondConverter;
29  import org.apache.syncope.core.persistence.api.search.SearchCondVisitor;
30  import org.apache.syncope.core.provisioning.api.data.FIQLQueryDataBinder;
31  import org.apache.syncope.core.spring.security.AuthContextUtils;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  public class FIQLQueryDataBinderImpl implements FIQLQueryDataBinder {
36  
37      protected static final Logger LOG = LoggerFactory.getLogger(FIQLQueryDataBinder.class);
38  
39      protected final SearchCondVisitor searchCondVisitor;
40  
41      protected final UserDAO userDAO;
42  
43      protected final EntityFactory entityFactory;
44  
45      public FIQLQueryDataBinderImpl(
46              final SearchCondVisitor searchCondVisitor,
47              final UserDAO userDAO,
48              final EntityFactory entityFactory) {
49  
50          this.searchCondVisitor = searchCondVisitor;
51          this.userDAO = userDAO;
52          this.entityFactory = entityFactory;
53      }
54  
55      @Override
56      public FIQLQuery create(final FIQLQueryTO fiqlQueryTO) {
57          FIQLQuery fiqlQuery = entityFactory.newEntity(FIQLQuery.class);
58  
59          fiqlQuery.setOwner(userDAO.findByUsername(AuthContextUtils.getUsername()));
60  
61          return update(fiqlQuery, fiqlQueryTO);
62      }
63  
64      @Override
65      public FIQLQuery update(final FIQLQuery fiqlQuery, final FIQLQueryTO fiqlQueryTO) {
66          fiqlQuery.setName(fiqlQueryTO.getName());
67          fiqlQuery.setTarget(fiqlQueryTO.getTarget());
68  
69          SearchCond cond = SearchCondConverter.convert(searchCondVisitor, fiqlQueryTO.getFiql());
70          if (!cond.isValid()) {
71              SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidSearchParameters);
72              sce.getElements().add(fiqlQueryTO.getFiql());
73              throw sce;
74          }
75          fiqlQuery.setFIQL(fiqlQueryTO.getFiql());
76  
77          return fiqlQuery;
78      }
79  
80      @Override
81      public FIQLQueryTO getFIQLQueryTO(final FIQLQuery fiqlQuery) {
82          FIQLQueryTO fiqlQueryTO = new FIQLQueryTO();
83  
84          fiqlQueryTO.setKey(fiqlQuery.getKey());
85          fiqlQueryTO.setName(fiqlQuery.getName());
86          fiqlQueryTO.setTarget(fiqlQuery.getTarget());
87          fiqlQueryTO.setFiql(fiqlQuery.getFIQL());
88  
89          return fiqlQueryTO;
90      }
91  }