From 01de09dcdf4a746bac66e1f700f8c6eb2c96c6ae Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Mon, 31 Jul 2017 17:31:42 +0500 Subject: - Rename QueryAdditional to QueryExtended - Use Introspector.decapitalize - Replace new lines with spaces - Fixed indentation - Fixed extended query name --- src/org/traccar/database/DataManager.java | 45 ++++++++++++++------------- src/org/traccar/database/QueryAdditional.java | 27 ---------------- src/org/traccar/database/QueryExtended.java | 27 ++++++++++++++++ 3 files changed, 50 insertions(+), 49 deletions(-) delete mode 100644 src/org/traccar/database/QueryAdditional.java create mode 100644 src/org/traccar/database/QueryExtended.java (limited to 'src/org/traccar/database') diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index a753c311b..3d89cc3d1 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -15,6 +15,7 @@ */ package org.traccar.database; +import java.beans.Introspector; import java.io.File; import java.lang.reflect.Method; import java.net.URL; @@ -124,7 +125,7 @@ public class DataManager { } } - public static String constructObjectQuery(String action, Class clazz, boolean additional) { + public static String constructObjectQuery(String action, Class clazz, boolean extended) { switch (action) { case ACTION_INSERT: case ACTION_UPDATE: @@ -136,17 +137,20 @@ public class DataManager { methods.removeAll(Arrays.asList(Object.class.getMethods())); methods.removeAll(Arrays.asList(BaseModel.class.getMethods())); for (Method method : methods) { - if (method.getName().startsWith("get") && method.getParameterTypes().length == 0 - && (additional ? method.isAnnotationPresent(QueryAdditional.class) - : !method.isAnnotationPresent(QueryIgnore.class) - && !method.isAnnotationPresent(QueryAdditional.class))) { - String name = method.getName().substring(3, 4).toLowerCase() - + method.getName().substring(4); + boolean skip; + if (extended) { + skip = !method.isAnnotationPresent(QueryExtended.class); + } else { + skip = method.isAnnotationPresent(QueryIgnore.class) + || method.isAnnotationPresent(QueryExtended.class); + } + if (!skip && method.getName().startsWith("get") && method.getParameterTypes().length == 0) { + String name = Introspector.decapitalize(method.getName().substring(3)); if (action.equals(ACTION_INSERT)) { fields.append(name).append(", "); values.append(":").append(name).append(", "); } else { - fields.append(name).append(" = :").append(name).append(",\n"); + fields.append(name).append(" = :").append(name).append(", "); } } } @@ -154,12 +158,12 @@ public class DataManager { if (action.equals(ACTION_INSERT)) { values.setLength(values.length() - 2); result.append("INSERT INTO ").append(getObjectsTableName(clazz)).append(" ("); - result.append(fields).append(")\n"); + result.append(fields).append(") "); result.append("VALUES (").append(values).append(")"); } else { - result.append("UPDATE ").append(getObjectsTableName(clazz)).append(" SET\n"); + result.append("UPDATE ").append(getObjectsTableName(clazz)).append(" SET "); result.append(fields); - result.append("\nWHERE id = :id"); + result.append(" WHERE id = :id"); } return result.toString(); case ACTION_SELECT_ALL: @@ -170,7 +174,7 @@ public class DataManager { return "DELETE FROM " + getObjectsTableName(clazz) + " WHERE id = :id"; default: throw new IllegalArgumentException("Unknown action"); - } + } } public static String constructPermissionQuery(String action, Class owner, Class property) { @@ -188,7 +192,7 @@ public class DataManager { + " AND " + makeNameId(property) + " = :" + makeNameId(property); default: throw new IllegalArgumentException("Unknown action"); - } + } } private String getQuery(String key) { @@ -203,16 +207,16 @@ public class DataManager { return getQuery(action, clazz, false); } - public String getQuery(String action, Class clazz, boolean additional) { + public String getQuery(String action, Class clazz, boolean extended) { String queryName; if (action.equals(ACTION_SELECT_ALL)) { queryName = "database.select" + clazz.getSimpleName() + "s"; } else { - queryName = "database." + action.toLowerCase() + clazz.getSimpleName(); + queryName = "database." + action.toLowerCase() + clazz.getSimpleName() + (extended ? "Extended" : ""); } String query = config.getString(queryName); if (query == null) { - query = constructObjectQuery(action, clazz, additional); + query = constructObjectQuery(action, clazz, extended); config.setString(queryName, query); } @@ -238,18 +242,15 @@ public class DataManager { } private static String getPermissionsTableName(Class owner, Class property) { - String ownerName = owner.getSimpleName(); String propertyName = property.getSimpleName(); if (propertyName.equals("ManagedUser")) { propertyName = "User"; } - return ownerName.substring(0, 1).toLowerCase() + ownerName.substring(1) + "_" - + propertyName.substring(0, 1).toLowerCase() + propertyName.substring(1); + return Introspector.decapitalize(owner.getSimpleName()) + "_" + Introspector.decapitalize(propertyName); } private static String getObjectsTableName(Class clazz) { - String name = clazz.getSimpleName(); - return name.substring(0, 1).toLowerCase() + name.substring(1) + "s"; + return Introspector.decapitalize(clazz.getSimpleName()) + "s"; } private void initDatabaseSchema() throws SQLException, LiquibaseException { @@ -399,7 +400,7 @@ public class DataManager { public static String makeNameId(Class clazz) { String name = clazz.getSimpleName(); - return name.substring(0, 1).toLowerCase() + name.substring(1) + (name.indexOf("Id") == -1 ? "Id" : ""); + return Introspector.decapitalize(name) + (name.indexOf("Id") == -1 ? "Id" : ""); } public Collection getPermissions(Class owner, Class property) diff --git a/src/org/traccar/database/QueryAdditional.java b/src/org/traccar/database/QueryAdditional.java deleted file mode 100644 index 7a42c1875..000000000 --- a/src/org/traccar/database/QueryAdditional.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.database; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target(ElementType.METHOD) -@Retention(RetentionPolicy.RUNTIME) -public @interface QueryAdditional { -} diff --git a/src/org/traccar/database/QueryExtended.java b/src/org/traccar/database/QueryExtended.java new file mode 100644 index 000000000..07bc2c211 --- /dev/null +++ b/src/org/traccar/database/QueryExtended.java @@ -0,0 +1,27 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.database; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface QueryExtended { +} -- cgit v1.2.3