source: trunk/fmgVen/src/com/fmguler/ven/util/Convert.java @ 26

Last change on this file since 26 was 26, checked in by fmguler, 13 years ago

Refs #3 - Ven.save() is transferred to the new package. Save use case is completed. Convert.toDB() converts camel case object names to database names with underscores. Unchecked VenException is thrown by Ven methods. Liquibase changelog (test-db-changelog.xml) is changed to include only a simple object to test save operation. It creates a sample database table and inserts a sample row. On rollback it removes sample data. Using liquibase, Sample.java builds database, tests save operation and rolls back.

QueryGenerator generates insert and update queries for save and generates sequence query to assign ids to new objects. Ven calls query generator and runs the query using spring jdbc template.

Old codebase updated to run at Java 1.4

File size: 2.2 KB
Line 
1/*
2 *  fmgVen - A Convention over Configuration Java ORM Tool
3 *  Copyright 2010 Fatih Mehmet Güler
4 *
5 *  Licensed under the Apache License, Version 2.0 (the "License");
6 *  you may not use this file except in compliance with the License.
7 *  You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 *  under the License.
17 */
18package com.fmguler.ven.util;
19
20import java.util.Locale;
21
22/**
23 * Utility class for conversions from database naming convention to java, and vice versa
24 * @author Fatih Mehmet Güler
25 */
26public class Convert {
27    /**
28     * Convert from java camelCase case to database naming convention
29     * <p>
30     * For example, if the name of the given name is SomeDomainObject
31     * it will be converted to some_domain_object
32     *
33     * @param camelCase the camelCase case name
34     * @return database style name
35     */
36    public static String toDB(String camelCase) {
37        if (camelCase.equals("")) return "";
38        StringBuffer result = new StringBuffer();
39        result.append(camelCase.substring(0, 1).toLowerCase(Locale.ENGLISH));
40        for (int i = 1; i < camelCase.length(); i++) {
41            String sCamel = camelCase.substring(i, i + 1);
42            String sLower = sCamel.toLowerCase(Locale.ENGLISH);
43            if (!sCamel.equals(sLower)) {
44                result.append("_").append(sLower);
45            } else result.append(sCamel);
46        }
47        return result.toString();
48    }
49
50    /**
51     * Convert the full package name to simple name.
52     * For Java 1.4 compatibility of Class.getSimpleName()
53     * @param fullName Class.getName()
54     * @return the part after the last .
55     */
56    public static String toSimpleName(String fullName) {
57        int i = fullName.lastIndexOf(".");
58        if (i < 0) return fullName;
59        return fullName.substring(i + 1);
60    }
61}
Note: See TracBrowser for help on using the repository browser.