You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by il...@apache.org on 2017/06/27 14:22:41 UTC
[2/2] syncope git commit: [SYNCOPE-1128] Now ordering table rows
before export, also for 'internal' foreign keys
[SYNCOPE-1128] Now ordering table rows before export, also for 'internal' foreign keys
Project: http://git-wip-us.apache.org/repos/asf/syncope/repo
Commit: http://git-wip-us.apache.org/repos/asf/syncope/commit/c12106de
Tree: http://git-wip-us.apache.org/repos/asf/syncope/tree/c12106de
Diff: http://git-wip-us.apache.org/repos/asf/syncope/diff/c12106de
Branch: refs/heads/master
Commit: c12106de4480d03c9b331ef72f5fa4a0bb65c246
Parents: 11ab8cb
Author: Francesco Chicchiriccò <il...@apache.org>
Authored: Tue Jun 27 16:21:50 2017 +0200
Committer: Francesco Chicchiriccò <il...@apache.org>
Committed: Tue Jun 27 16:22:24 2017 +0200
----------------------------------------------------------------------
.../jpa/content/XMLContentExporter.java | 82 ++++++++++++++------
.../jpa/outer/XMLContentExporterTest.java | 69 ++++++++++++++++
2 files changed, 127 insertions(+), 24 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/syncope/blob/c12106de/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/content/XMLContentExporter.java
----------------------------------------------------------------------
diff --git a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/content/XMLContentExporter.java b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/content/XMLContentExporter.java
index 3fba66b..f1f3e44 100644
--- a/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/content/XMLContentExporter.java
+++ b/core/persistence-jpa/src/main/java/org/apache/syncope/core/persistence/jpa/content/XMLContentExporter.java
@@ -227,7 +227,11 @@ public class XMLContentExporter extends AbstractContentDealer implements Content
return res;
}
- private void doExportTable(final TransformerHandler handler, final Connection conn, final String tableName,
+ private void doExportTable(
+ final TransformerHandler handler,
+ final String dbSchema,
+ final Connection conn,
+ final String tableName,
final String whereClause) throws SQLException, SAXException {
LOG.debug("Export table {}", tableName);
@@ -236,24 +240,58 @@ public class XMLContentExporter extends AbstractContentDealer implements Content
PreparedStatement stmt = null;
ResultSet rs = null;
- ResultSet pkeyRS = null;
try {
- // ------------------------------------
- // retrieve primary keys to perform an ordered select
-
- final DatabaseMetaData meta = conn.getMetaData();
- pkeyRS = meta.getPrimaryKeys(null, null, tableName);
+ StringBuilder orderBy = new StringBuilder();
- final StringBuilder orderBy = new StringBuilder();
+ DatabaseMetaData meta = conn.getMetaData();
- while (pkeyRS.next()) {
- final String columnName = pkeyRS.getString("COLUMN_NAME");
- if (columnName != null) {
- if (orderBy.length() > 0) {
- orderBy.append(",");
+ // ------------------------------------
+ // retrieve foreign keys (linked to the same table) to perform an ordered select
+ ResultSet pkeyRS = null;
+ try {
+ pkeyRS = meta.getImportedKeys(conn.getCatalog(), dbSchema, tableName);
+ while (pkeyRS.next()) {
+ if (tableName.equals(pkeyRS.getString("PKTABLE_NAME"))) {
+ String columnName = pkeyRS.getString("FKCOLUMN_NAME");
+ if (columnName != null) {
+ if (orderBy.length() > 0) {
+ orderBy.append(",");
+ }
+
+ orderBy.append(columnName);
+ }
}
+ }
+ } finally {
+ if (pkeyRS != null) {
+ try {
+ pkeyRS.close();
+ } catch (SQLException e) {
+ LOG.error("While closing result set", e);
+ }
+ }
+ }
- orderBy.append(columnName);
+ // retrieve primary keys to perform an ordered select
+ try {
+ pkeyRS = meta.getPrimaryKeys(null, null, tableName);
+ while (pkeyRS.next()) {
+ String columnName = pkeyRS.getString("COLUMN_NAME");
+ if (columnName != null) {
+ if (orderBy.length() > 0) {
+ orderBy.append(",");
+ }
+
+ orderBy.append(columnName);
+ }
+ }
+ } finally {
+ if (pkeyRS != null) {
+ try {
+ pkeyRS.close();
+ } catch (SQLException e) {
+ LOG.error("While closing result set", e);
+ }
}
}
@@ -299,13 +337,6 @@ public class XMLContentExporter extends AbstractContentDealer implements Content
LOG.error("While closing result set", e);
}
}
- if (pkeyRS != null) {
- try {
- pkeyRS.close();
- } catch (SQLException e) {
- LOG.error("While closing result set", e);
- }
- }
if (stmt != null) {
try {
stmt.close();
@@ -352,7 +383,8 @@ public class XMLContentExporter extends AbstractContentDealer implements Content
throw new IllegalArgumentException("Could not find DataSource for domain " + domain);
}
- String dbSchema = ApplicationContextProvider.getBeanFactory().getBean(domain + "DatabaseSchema", String.class);
+ String dbSchema = ApplicationContextProvider.getBeanFactory().getBean(domain + "DatabaseSchema",
+ String.class);
Connection conn = null;
ResultSet rs = null;
@@ -360,7 +392,8 @@ public class XMLContentExporter extends AbstractContentDealer implements Content
conn = DataSourceUtils.getConnection(dataSource);
final DatabaseMetaData meta = conn.getMetaData();
- rs = meta.getTables(null, StringUtils.isBlank(dbSchema) ? null : dbSchema, null, new String[] { "TABLE" });
+ rs = meta.getTables(null, StringUtils.isBlank(dbSchema) ? null : dbSchema, null,
+ new String[] { "TABLE" });
final Set<String> tableNames = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
@@ -377,7 +410,8 @@ public class XMLContentExporter extends AbstractContentDealer implements Content
// then sort tables based on foreign keys and dump
for (String tableName : sortByForeignKeys(dbSchema, conn, tableNames)) {
try {
- doExportTable(handler, conn, tableName, TABLES_TO_BE_FILTERED.get(tableName.toUpperCase()));
+ doExportTable(
+ handler, dbSchema, conn, tableName, TABLES_TO_BE_FILTERED.get(tableName.toUpperCase()));
} catch (Exception e) {
LOG.error("Failure exporting table {}", tableName, e);
}
http://git-wip-us.apache.org/repos/asf/syncope/blob/c12106de/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/XMLContentExporterTest.java
----------------------------------------------------------------------
diff --git a/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/XMLContentExporterTest.java b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/XMLContentExporterTest.java
new file mode 100644
index 0000000..84bc767
--- /dev/null
+++ b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/outer/XMLContentExporterTest.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.syncope.core.persistence.jpa.outer;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.collections4.Predicate;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.output.ByteArrayOutputStream;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.syncope.core.persistence.api.content.ContentExporter;
+import org.apache.syncope.core.persistence.jpa.AbstractTest;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.transaction.annotation.Transactional;
+
+@Transactional("Master")
+public class XMLContentExporterTest extends AbstractTest {
+
+ @Autowired
+ private ContentExporter exporter;
+
+ @Test
+ public void issueSYNCOPE1128() throws Exception {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ exporter.export("Master", baos, null, null, null);
+
+ String exported = baos.toString(Charset.defaultCharset());
+ assertTrue(StringUtils.isNotBlank(exported));
+
+ List<String> realms = CollectionUtils.select(
+ IOUtils.readLines(IOUtils.toInputStream(exported, Charset.defaultCharset()), Charset.defaultCharset()),
+ new Predicate<String>() {
+
+ @Override
+ public boolean evaluate(final String row) {
+ return row.startsWith("<REALM");
+ }
+ }, new ArrayList<String>());
+ assertEquals(4, realms.size());
+ assertTrue(realms.get(0).contains("NAME=\"/\""));
+ assertTrue(realms.get(1).contains("NAME=\"two\""));
+ assertTrue(realms.get(2).contains("NAME=\"odd\""));
+ assertTrue(realms.get(3).contains("NAME=\"even\""));
+ }
+
+}