changeset 24:056f8e1e995e database-connection-manager

Properly record history in fromRepository constructors
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Mon, 05 Dec 2011 16:20:06 +0100
parents a71a40911c27
children 79dc7091dbbc
files m-toolbox/classes/+utils/@repository/adjustPlist.m m-toolbox/classes/+utils/@repository/repository.m m-toolbox/classes/@collection/fromRepository.m m-toolbox/classes/@ltpda_uo/fromRepository.m
diffstat 4 files changed, 67 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/m-toolbox/classes/+utils/@repository/adjustPlist.m	Mon Dec 05 16:20:06 2011 +0100
@@ -0,0 +1,37 @@
+function adjustPlist(conn, pl)
+% ADJUSTPLIST(CONN, PL) Removes CONN, USERNAME, PASSWORD parameters
+% from plist PL, and adds or or substitutes HOSTNAME and DATABASE
+% parameters with the ones used to establish connection CONN.
+%
+% The resulting plist may be used to set object history.
+
+  % check parameters
+  if ~isa(conn, 'java.sql.Connection')
+    error('### invalid call');
+  end
+  if ~isa(pl, 'plist')
+    error('### invalid call');
+  end
+
+  % get connection parameters
+  r = '^jdbc:mysql://(?<hostname>.+)/(?<database>.+)$';
+  c = regexp(char(conn.getMetaData().getURL()), r, 'names');
+
+  % remove unwanted parameters
+  prem(pl, 'conn');
+  prem(pl, 'username');
+  prem(pl, 'password');
+
+  % add essentials connections parameters
+  pset(pl, 'hostname', c.hostname);
+  pset(pl, 'database', c.database);
+
+end
+
+
+function prem(pl, key)
+% PREM Remove parameter KEY if present in plist PL.
+  if isparam(pl, key)
+    remove(pl, key);
+  end
+end
--- a/m-toolbox/classes/+utils/@repository/repository.m	Mon Dec 05 16:20:06 2011 +0100
+++ b/m-toolbox/classes/+utils/@repository/repository.m	Mon Dec 05 16:20:06 2011 +0100
@@ -2,6 +2,7 @@
 
   methods (Static)
 
+    adjustPlist(conn, pl);
     type = getObjectType(conn, id);
     ids = getCollectionIDs(conn, cid);
     varargout = getUser(conn);
--- a/m-toolbox/classes/@collection/fromRepository.m	Mon Dec 05 16:20:06 2011 +0100
+++ b/m-toolbox/classes/@collection/fromRepository.m	Mon Dec 05 16:20:06 2011 +0100
@@ -30,8 +30,7 @@
 
   collFound  = false;
 
-  %%% check if using binary or not: 'yes'/'no' or true/false or
-  %%% 'true'/'false'
+  % check if using binary download
   bin = utils.prog.yes2true(bin);
 
   % database connection
@@ -65,10 +64,8 @@
         robj = ltpda_uo.retrieve(conn, id);
       end
 
-      %---- remove the connection from the history plist
-      if plh.isparam('conn')
-        plh.remove('conn');
-      end
+      %---- Set connection parameters in the plist
+      utils.repository.adjustPlist(conn, plh);
 
       %---- Set only the ID of the current object to the plist
       plh.pset('ID', id);
--- a/m-toolbox/classes/@ltpda_uo/fromRepository.m	Mon Dec 05 16:20:06 2011 +0100
+++ b/m-toolbox/classes/@ltpda_uo/fromRepository.m	Mon Dec 05 16:20:06 2011 +0100
@@ -13,19 +13,19 @@
 %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 function [objs, plhout, ii] = fromRepository(obj, pl, ii)
-  
+
   VERSION = 'ltpda_uo: $Id: fromRepository.m,v 1.4 2010/03/16 19:16:20 ingo Exp $';
-  
+
   requested_class = class(obj);
-  
+
   % Set the method version string in the minfo object
   ii.setMversion([VERSION '-->' ii.mversion]);
-    
+
   % Get parameters
   ids      = find(pl, 'id');
   cids     = find(pl, 'cid');
   bin      = find(pl, 'binary');
-  
+
   % Make sure that 'ids' or 'cids' are empty arrays if they are empty.
   % It might be that the GUI return an empty string.
   if isempty(ids)
@@ -34,19 +34,18 @@
   if isempty(cids)
     cids = [];
   end
-  
+
   % Check if some ID is defined
   if isempty(ids) && isempty(cids)
     error('### Please define at least one object ID or connection ID');
   end
-  
-  %%% check if using binary or not: 'yes'/'no' or true/false or
-  %%% 'true'/'false'
+
+  % check if using binary download
   bin = utils.prog.yes2true(bin);
 
   % database connection
   conn = LTPDADatabaseConnectionManager().connect(pl);
-  
+
   if ~isempty(cids)
     for kk=1:numel(cids)
       cid = cids(kk);
@@ -54,21 +53,21 @@
       ids = [ids utils.repository.getCollectionIDs(conn, cid)];
     end
   end
-  
+
   % Get each ID
   Nids = length(ids);
   objs = [];
   plhout = [];
-  
+
   for kk=1:Nids
-    
+
     %---- copy the input plist because each object should get an other plist
     plh = copy(pl, 1);
-    
+
     %---- This id
     id = ids(kk);
     utils.helper.msg(utils.const.msg.OPROC2, 'retrieving ID %d', id);
-    
+
     try
       %---- call database constructor
       if bin
@@ -76,25 +75,23 @@
       else
         obj = ltpda_uo.retrieve(conn, id);
       end
-      
+
       if ~strcmp(class(obj), requested_class)
         error('### You have used the constructor ''%s'' but the object with id=%d is of class ''%s''', requested_class, id, class(obj));
       end
-      
-      %---- remove the connection from the history plist
-      if plh.isparam('conn')
-        plh.remove('conn');
-      end
-      
+
+      %---- Set connection parameters in the plist
+      utils.repository.adjustPlist(conn, plh);
+
       %---- Set only the ID of the current object to the plist
-      plh.pset('ID', id);
-      
+      plh.pset('id', id);
+
       %---- Add history-plist to output array
       plhout = [plhout plh];
-      
+
       %---- Add to output array
       objs = [objs obj];
-      
+
     catch ex
       % close connection if we own it
       if isempty(find(pl, 'conn'))
@@ -102,13 +99,13 @@
       end
       throw(ex)
     end
-      
+
   end
-  
+
   % close connection if we own it
   if isempty(find(pl, 'conn'))
     conn.close();
   end
-  
+
 end