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.persistence.jpa.openjpa;
20  
21  import java.util.Optional;
22  import org.apache.commons.lang3.BooleanUtils;
23  import org.apache.openjpa.jdbc.identifier.DBIdentifier;
24  import org.apache.openjpa.jdbc.kernel.JDBCStore;
25  import org.apache.openjpa.jdbc.meta.ValueMapping;
26  import org.apache.openjpa.jdbc.meta.strats.AbstractValueHandler;
27  import org.apache.openjpa.jdbc.schema.Column;
28  import org.apache.openjpa.jdbc.schema.ColumnIO;
29  import org.apache.openjpa.jdbc.sql.DBDictionary;
30  import org.apache.openjpa.meta.JavaTypes;
31  
32  public class BooleanValueHandler extends AbstractValueHandler {
33  
34      private static final long serialVersionUID = -6742506201236646294L;
35  
36      private static final BooleanValueHandler INSTANCE = new BooleanValueHandler();
37  
38      public static BooleanValueHandler getInstance() {
39          return INSTANCE;
40      }
41  
42      /**
43       * @deprecated
44       */
45      @Override
46      @Deprecated
47      public Column[] map(final ValueMapping vm, final String name, final ColumnIO io, final boolean adapt) {
48          DBDictionary dict = vm.getMappingRepository().getDBDictionary();
49          DBIdentifier colName = DBIdentifier.newColumn(name, Optional.ofNullable(dict).
50                  filter(DBDictionary::delimitAll).isPresent());
51          return map(colName, io, adapt);
52      }
53  
54      public static Column[] map(final DBIdentifier name, final ColumnIO io, final boolean adapt) {
55          Column col = new Column();
56          col.setIdentifier(name);
57          col.setJavaType(JavaTypes.INT);
58          return new Column[] { col };
59      }
60  
61      @Override
62      public Object toDataStoreValue(final ValueMapping vm, final Object val, final JDBCStore store) {
63          return Optional.ofNullable(val).map(o -> BooleanUtils.isTrue((Boolean) o) ? 1 : 0).orElse(null);
64      }
65  
66      @Override
67      public Object toObjectValue(final ValueMapping vm, final Object val) {
68          return Optional.ofNullable(val).map(o -> BooleanUtils.toBoolean((int) o)).orElse(null);
69      }
70  }