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