|
| 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, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + * |
| 19 | + */ |
| 20 | +package org.apache.polygene.sample.sqlsupport; |
| 21 | + |
| 22 | +import java.sql.Connection; |
| 23 | +import java.sql.SQLException; |
| 24 | +import java.sql.Statement; |
| 25 | +import javax.sql.DataSource; |
| 26 | +import org.apache.polygene.api.entity.EntityBuilder; |
| 27 | +import org.apache.polygene.api.query.Query; |
| 28 | +import org.apache.polygene.api.query.QueryBuilder; |
| 29 | +import org.apache.polygene.api.structure.Application; |
| 30 | +import org.apache.polygene.api.structure.Module; |
| 31 | +import org.apache.polygene.api.unitofwork.UnitOfWork; |
| 32 | +import org.apache.polygene.bootstrap.Energy4Java; |
| 33 | +import org.apache.polygene.entitystore.sql.assembly.PostgreSQLEntityStoreAssembler; |
| 34 | +import org.apache.polygene.index.sql.assembly.PostgreSQLIndexQueryAssembler; |
| 35 | +import org.apache.polygene.library.sql.common.SQLConfiguration; |
| 36 | +import org.apache.polygene.library.sql.common.SQLUtil; |
| 37 | + |
| 38 | +import static org.apache.polygene.api.query.QueryExpressions.eq; |
| 39 | +import static org.apache.polygene.api.query.QueryExpressions.templateFor; |
| 40 | + |
| 41 | +/** |
| 42 | + * SQL Support Sample Main Class. |
| 43 | + * <p><strong>psql postgres</strong></p> |
| 44 | + * <p>CREATE USER jdbc_test_login WITH PASSWORD 'password';</p> |
| 45 | + * <p>CREATE DATABASE jdbc_test_db;</p> |
| 46 | + * <p>GRANT ALL PRIVILEGES ON DATABASE jdbc_test_db TO jdbc_test_login;</p> |
| 47 | + * <p><strong>psql -d jdbc_test_db</strong></p> |
| 48 | + * <p>CREATE EXTENSION ltree;</p> |
| 49 | + */ |
| 50 | +public class Main |
| 51 | +{ |
| 52 | + |
| 53 | + public static void main( String[] args ) |
| 54 | + throws Exception |
| 55 | + { |
| 56 | + final Application application = new Energy4Java().newApplication( new AppAssembler() ); |
| 57 | + application.activate(); |
| 58 | + Runtime.getRuntime().addShutdownHook( new Thread( new Runnable() |
| 59 | + { |
| 60 | + @Override |
| 61 | + @SuppressWarnings( "CallToThreadDumpStack" ) |
| 62 | + public void run() |
| 63 | + { |
| 64 | + try { |
| 65 | + application.passivate(); |
| 66 | + } catch ( Exception ex ) { |
| 67 | + System.err.println( "Unable to passivate Polygene application!" ); |
| 68 | + ex.printStackTrace(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + } ) ); |
| 73 | + Module domainModule = application.findModule( "app", "domain" ); |
| 74 | + |
| 75 | + int exitStatus = 0; |
| 76 | + |
| 77 | + try { |
| 78 | + |
| 79 | + UnitOfWork uow = domainModule.unitOfWorkFactory().newUnitOfWork(); |
| 80 | + EntityBuilder<PretextEntity> builder = uow.newEntityBuilder( PretextEntity.class ); |
| 81 | + PretextEntity pretext = builder.instance(); |
| 82 | + pretext.reason().set( "Testing purpose" ); |
| 83 | + builder.newInstance(); |
| 84 | + uow.complete(); |
| 85 | + |
| 86 | + uow = domainModule.unitOfWorkFactory().newUnitOfWork(); |
| 87 | + QueryBuilder<PretextEntity> queryBuilder = domainModule.newQueryBuilder( PretextEntity.class ); |
| 88 | + queryBuilder = queryBuilder.where( eq( templateFor( PretextEntity.class ).reason(), "Testing purpose" ) ); |
| 89 | + Query<PretextEntity> query = uow.newQuery( queryBuilder ); |
| 90 | + pretext = query.find(); |
| 91 | + if ( pretext == null ) { |
| 92 | + System.err.println( "ERROR: Unable to find pretext!" ); |
| 93 | + exitStatus = -1; |
| 94 | + } else { |
| 95 | + System.out.println( "SUCCESS: Found Pretext with reason: " + pretext.reason().get() ); |
| 96 | + } |
| 97 | + uow.discard(); |
| 98 | + |
| 99 | + } finally { |
| 100 | + deleteData( application.findModule( "infra", "persistence" ) ); |
| 101 | + } |
| 102 | + |
| 103 | + System.exit( exitStatus ); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Completely delete data so the sample can be run multiple times. |
| 108 | + */ |
| 109 | + private static void deleteData( Module persistenceModule ) |
| 110 | + throws SQLException |
| 111 | + { |
| 112 | + // EntityStore Data |
| 113 | + { |
| 114 | + UnitOfWork uow = persistenceModule.unitOfWorkFactory().newUnitOfWork(); |
| 115 | + try { |
| 116 | + SQLConfiguration config = uow.get( SQLConfiguration.class, PostgreSQLEntityStoreAssembler.DEFAULT_ENTITYSTORE_IDENTITY ); |
| 117 | + Connection connection = persistenceModule.findService( DataSource.class ).get().getConnection(); |
| 118 | + connection.setAutoCommit( false ); |
| 119 | + connection.setReadOnly( false ); |
| 120 | + String schemaName = config.schemaName().get(); |
| 121 | + |
| 122 | + Statement stmt = null; |
| 123 | + try { |
| 124 | + stmt = connection.createStatement(); |
| 125 | + stmt.execute( "DROP SCHEMA " + schemaName + " CASCADE" ); |
| 126 | + connection.commit(); |
| 127 | + } finally { |
| 128 | + SQLUtil.closeQuietly( stmt ); |
| 129 | + } |
| 130 | + } finally { |
| 131 | + uow.discard(); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // Indexing Data |
| 136 | + { |
| 137 | + UnitOfWork uow = persistenceModule.unitOfWorkFactory().newUnitOfWork(); |
| 138 | + try { |
| 139 | + SQLConfiguration config = uow.get( SQLConfiguration.class, PostgreSQLIndexQueryAssembler.DEFAULT_IDENTITY ); |
| 140 | + Connection connection = persistenceModule.findService( DataSource.class ).get().getConnection(); |
| 141 | + connection.setAutoCommit( false ); |
| 142 | + connection.setReadOnly( false ); |
| 143 | + String schemaName = config.schemaName().get(); |
| 144 | + |
| 145 | + Statement stmt = null; |
| 146 | + try { |
| 147 | + stmt = connection.createStatement(); |
| 148 | + stmt.execute( "DROP SCHEMA " + schemaName + " CASCADE" ); |
| 149 | + connection.commit(); |
| 150 | + } finally { |
| 151 | + SQLUtil.closeQuietly( stmt ); |
| 152 | + SQLUtil.closeQuietly( connection ); |
| 153 | + } |
| 154 | + } finally { |
| 155 | + uow.discard(); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | +} |
0 commit comments