@@ -147,54 +147,59 @@ public RetailCustomer importResource(InputStream stream) {
147147
148148 @ Transactional
149149 @ Override
150- public Subscription associateByUUID (Long retailCustomerId , UUID uuId ) {
150+ public Subscription associateByUUID (Long retailCustomerId , UUID uuid ) {
151151 Subscription subscription = null ;
152- UsagePoint usagePoint = new UsagePoint ();
153- usagePoint .setUUID (uuId );
154-
155- RetailCustomer retailCustomer = findById (retailCustomerId );
156- usagePoint .setServiceCategory (new ServiceCategory (ServiceCategory .ELECTRICITY_SERVICE ));
157- usagePoint .setRetailCustomer (retailCustomer );
158- usagePointService .createOrReplaceByUUID (usagePoint );
159-
160- // now retrieve the result and use it for any pending subscriptions
161- usagePoint = usagePointService .findByUUID (uuId );
162-
163- // now see if there are any authorizations for this information
164- //
165- try {
166-
167- List <Authorization > authorizationList = authorizationService .findAllByRetailCustomerId (retailCustomer .getId ());
168- Iterator <Authorization > authorizationIterator = authorizationList .iterator ();
169-
170- while (authorizationIterator .hasNext ()) {
171-
172- Authorization authorization = authorizationIterator .next ();
173- subscription = subscriptionService .findByAuthorizationId (authorization .getId ());
174- String resourceUri = authorization .getResourceURI ();
175- if (resourceUri == null ) {
176-
177- // this is the first time this authorization has been in effect. We
178- // must set up the appropriate resource links
179- ApplicationInformation applicationInformation = authorization .getApplicationInformation ();
180- resourceUri = applicationInformation .getDataCustodianResourceEndpoint ();
181- resourceUri = resourceUri + "/Batch/Subscription/" + subscription .getId ();
182- authorization .setResourceURI (resourceUri );
183- }
184-
185- // make sure the UsagePoints we just imported are linked up with
186- // the subscription if any
187- subscription = subscriptionService .addUsagePoint (subscription , usagePoint );
188- resourceService .persist (subscription );
189- resourceService .persist (usagePoint );
190- }
191- } catch (Exception e ){
192- // we don't expect any problems here, and if we do have an exception,
193- // it will rollback the transaction.
194- e .printStackTrace ();
195- return null ;
196- }
152+ RetailCustomer retailCustomer = null ;
153+ UsagePoint usagePoint = null ;
197154
155+ try {
156+ retailCustomer = resourceService .findById (retailCustomerId , RetailCustomer .class );
157+
158+ usagePoint = resourceService .findByUUID (uuid , UsagePoint .class );
159+
160+ if (usagePoint == null ) {
161+
162+ usagePoint = new UsagePoint ();
163+ usagePoint .setUUID (uuid );
164+ usagePoint .setDescription ("A Temporary UsagePoint Description" );
165+ resourceService .persist (usagePoint );
166+
167+ }
168+ usagePoint .setRetailCustomer (retailCustomer );
169+ resourceService .merge (usagePoint );
170+
171+
172+ // now see if there are any authorizations for this information
173+ //
174+ try {
175+
176+ for (Authorization authorization : authorizationService .findAllByRetailCustomerId (retailCustomer .getId ())) {
177+
178+
179+ String resourceUri = authorization .getResourceURI ();
180+ if (resourceUri == null ) {
181+
182+ authorization .setResourceURI (authorization .getApplicationInformation ().getDataCustodianResourceEndpoint ()
183+ + "/Batch/Subscription/" + subscription .getId ());
184+ resourceService .merge (authorization );
185+
186+ }
187+
188+ subscription = subscriptionService .findByAuthorizationId (authorization .getId ());
189+
190+ subscription .getUsagePoints ().add (usagePoint );
191+ resourceService .merge (subscription );
192+ }
193+ } catch (Exception e ){
194+ // we get here if we don't have a subscription
195+ return null ;
196+ }
197+
198+
199+ } catch (Exception e ) {
200+ System .out .printf ("****Error Associating UsagePoint: %s - %s\n " , retailCustomer .toString (), usagePoint .toString ());
201+ }
202+
198203 return subscription ;
199204 }
200205
0 commit comments