2020
2121namespace AppserverIo \Apps \Example \Services ;
2222
23- use Doctrine \DBAL \DriverManager ;
24- use Doctrine \ORM \Tools \SchemaTool ;
25- use Doctrine \DBAL \Schema \SqliteSchemaManager ;
2623use Doctrine \Common \Collections \ArrayCollection ;
2724use AppserverIo \Apps \Example \Entities \Impl \Product ;
2825use AppserverIo \Psr \EnterpriseBeans \Annotations as EPB ;
26+ use AppserverIo \Console \Server \Services \SchemaProcessorTrait ;
2927
3028/**
3129 * A singleton session bean implementation that handles the
@@ -43,27 +41,43 @@ class SchemaProcessor extends AbstractPersistenceProcessor implements SchemaProc
4341{
4442
4543 /**
46- * The name of the configuration key that contains the database name .
44+ * Trait that provides schema managing functionality .
4745 *
48- * @var string
46+ * @var AppserverIo\Console\Server\Services\SchemaProcessorTrait
4947 */
50- const PARAM_DBNAME = 'dbname ' ;
51-
48+ use SchemaProcessorTrait;
49+
50+ /**
51+ * The application instance.
52+ *
53+ * @var \AppserverIo\Psr\Application\ApplicationInterface
54+ * @EPB\Resource(type="ApplicationInterface")
55+ */
56+ protected $ application ;
57+
5258 /**
53- * The DIC provider instance.
59+ * The DI provider instance.
5460 *
55- * @var \AppserverIo\Psr\Di\ProviderInterface $provider
61+ * @var \AppserverIo\Psr\Di\ProviderInterface
5662 * @EPB\Resource(type="ProviderInterface")
5763 */
58- protected $ providerInterface ;
59-
64+ protected $ provider ;
65+
66+ /**
67+ * The timer service context instance.
68+ *
69+ * @var \AppserverIo\Psr\EnterpriseBeans\TimerServiceContextInterface
70+ * @EPB\Resource(type="TimerServiceContextInterface")
71+ */
72+ protected $ timerServiceContext ;
73+
6074 /**
61- * The system logger implementation .
75+ * The object manager instance .
6276 *
63- * @var \AppserverIo\Logger\Logger
64- * @EPB\Resource(type="SystemLogger ")
77+ * @var \AppserverIo\Psr\Di\ObjectManagerInterface
78+ * @EPB\Resource(type="ObjectManagerInterface ")
6579 */
66- protected $ systemLogger ;
80+ protected $ objectManager ;
6781
6882 /**
6983 * A list with default credentials for login testing.
@@ -83,95 +97,45 @@ class SchemaProcessor extends AbstractPersistenceProcessor implements SchemaProc
8397 array ('appserver_09 ' , 'appserver.i0 ' , array ('Customer ' )),
8498 array ('guest ' , 'appserver.i0 ' , array ('Guest ' ))
8599 );
86-
100+
87101 /**
88- * Example method that should be invoked after constructor .
102+ * Returns the application instance .
89103 *
90- * @return void
91- * @EPB\PostConstruct
104+ * @return \AppserverIo\Psr\Application\ApplicationInterface The initialized Doctrine entity manager
92105 */
93- public function initialize ()
106+ protected function getApplication ()
94107 {
95- \info (
96- sprintf ('%s has successfully been invoked by @PostConstruct annotation ' , __METHOD__ )
97- );
108+ return $ this ->application ;
98109 }
99-
110+
100111 /**
101- * Return's the system logger instance.
112+ * Returns the DI provider instance.
102113 *
103- * @return \AppserverIo\Logger\Logger The sytsem logger instance
114+ * @return \AppserverIo\Psr\Di\ProviderInterface The DI provider instance
104115 */
105- public function getSystemLogger ()
116+ protected function getProvider ()
106117 {
107- return $ this ->systemLogger ;
118+ return $ this ->provider ;
108119 }
109-
120+
110121 /**
111- * Create's the database itself .
122+ * Returns the object manager instance .
112123 *
113- * This quite seems to be a bit strange, because with all databases
114- * other than SQLite, we need to remove the database name from the
115- * connection parameters BEFORE connecting to the database, as
116- * connection to a not existing database fails.
117- *
118- * @return void
124+ * @return \AppserverIo\Psr\Di\ObjectManagerInterface The object manager instance
119125 */
120- public function createDatabase ()
126+ protected function getObjectManager ()
121127 {
122-
123- try {
124- // clone the connection and load the database name
125- $ connection = clone $ this ->getEntityManager ()->getConnection ();
126- $ dbname = $ connection ->getDatabase ();
127-
128- // remove the the database name
129- $ params = $ connection ->getParams ();
130- if (isset ($ params [SchemaProcessor::PARAM_DBNAME ])) {
131- unset($ params [SchemaProcessor::PARAM_DBNAME ]);
132- }
133-
134- // create a new connection WITHOUT the database name
135- $ cn = DriverManager::getConnection ($ params );
136- $ sm = $ cn ->getDriver ()->getSchemaManager ($ cn );
137-
138- // SQLite doesn't support database creation by a method
139- if ($ sm instanceof SqliteSchemaManager) {
140- return ;
141- }
142-
143- // query whether or not the database already exists
144- if (!in_array ($ dbname , $ sm ->listDatabases ())) {
145- $ sm ->createDatabase ($ dbname );
146- }
147- } catch (\Exception $ e ) {
148- \error ($ e ->__toString ());
149- }
128+ return $ this ->objectManager ;
150129 }
151-
130+
152131 /**
153- * Deletes the database schema and creates it new.
154- *
155- * Attention: All data will be lost if this method has been invoked.
132+ * Returns the timer service context instance.
156133 *
157- * @return void
134+ * @return \AppserverIo\Psr\EnterpriseBeans\TimerServiceContextInterface The timer service context instance
158135 */
159- public function createSchema ()
136+ protected function getTimerServiceContext ()
160137 {
161-
162- try {
163- // load the entity manager and the schema tool
164- $ entityManager = $ this ->getEntityManager ();
165- $ schemaTool = new SchemaTool ($ entityManager );
166-
167- // load the class definitions
168- $ classes = $ entityManager ->getMetadataFactory ()->getAllMetadata ();
169-
170- // create or update the schema
171- $ schemaTool ->updateSchema ($ classes );
172- } catch (\Exception $ e ) {
173- \error ($ e ->__toString ());
174- }
138+ return $ this ->timerServiceContext ;
175139 }
176140
177141 /**
@@ -196,7 +160,7 @@ public function createDefaultProducts()
196160 }
197161
198162 // set user data and save it
199- $ product = $ this ->providerInterface ->newInstance ($ className );
163+ $ product = $ this ->getProvider () ->newInstance ($ className );
200164 $ product ->setName ("Product- $ i " );
201165 $ product ->setStatus (Product::STATUS_ACTIVE );
202166 $ product ->setUrlKey ("product- $ i " );
@@ -237,7 +201,7 @@ public function createDefaultCredentials()
237201 }
238202
239203 // set user data and save it
240- $ user = $ this ->providerInterface ->newInstance ($ className );
204+ $ user = $ this ->getProvider () ->newInstance ($ className );
241205 $ user->
setEmail (
sprintf (
'%[email protected] ' ,
$ username));
242206 $ user ->setUsername ($ username );
243207 $ user ->setUserLocale ('en_US ' );
@@ -253,7 +217,7 @@ public function createDefaultCredentials()
253217
254218 // create the user's roles
255219 foreach ($ roleNames as $ roleName ) {
256- $ role = $ this ->providerInterface ->newInstance ('\AppserverIo\Apps\Example\Entities\Impl\Role ' );
220+ $ role = $ this ->getProvider () ->newInstance ('\AppserverIo\Apps\Example\Entities\Impl\Role ' );
257221 $ role ->setUser ($ user );
258222 $ role ->setName ($ roleName );
259223 $ roles ->add ($ role );
0 commit comments