From: <tic...@us...> - 2008-05-30 14:32:27
|
Revision: 1310 http://geonetwork.svn.sourceforge.net/geonetwork/?rev=1310&view=rev Author: ticheler Date: 2008-05-30 07:32:31 -0700 (Fri, 30 May 2008) Log Message: ----------- - Improved migration process for transition from version 2.0 to 2.2. Main properties: - try to identify the proper metadata owner by extracting the editor from the (ISO19115:DIS) metadata and match it against the user database. - on failure, match against first editor of first group with edit or admin privileges - on failure assign to given default user & group Modified Paths: -------------- trunk/src/org/fao/gast/gui/panels/migration/oldinst/MainPanel.java trunk/src/org/fao/gast/gui/panels/migration/oldinst/Worker.java Added Paths: ----------- trunk/src/org/fao/gast/gui/panels/migration/oldinst/UserDialog.java Modified: trunk/src/org/fao/gast/gui/panels/migration/oldinst/MainPanel.java =================================================================== --- trunk/src/org/fao/gast/gui/panels/migration/oldinst/MainPanel.java 2008-05-30 14:30:55 UTC (rev 1309) +++ trunk/src/org/fao/gast/gui/panels/migration/oldinst/MainPanel.java 2008-05-30 14:32:31 UTC (rev 1310) @@ -27,6 +27,7 @@ import java.awt.event.ActionEvent; import java.io.File; import javax.swing.JButton; +import javax.swing.JCheckBox; import javax.swing.JComponent; import javax.swing.JFileChooser; import javax.swing.JLabel; @@ -36,6 +37,7 @@ import org.dlib.gui.GuiUtil; import org.dlib.gui.ProgressDialog; import org.fao.gast.gui.panels.FormPanel; +import org.fao.gast.lib.Lib; //============================================================================== @@ -62,12 +64,20 @@ public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); + Object source = e.getSource(); if (cmd.equals("browse")) browse(); else if (cmd.equals("migrate")) migrate(); + + else if (source.equals(jcbCreateUser)) + { + boolean enable = jcbCreateUser.isSelected(); + txtUser.setEnabled(enable); + txtGroup.setEnabled(enable); + } } //--------------------------------------------------------------------------- @@ -90,6 +100,29 @@ ProgressDialog dialog = new ProgressDialog(owner, "Migrating data"); Worker worker = new Worker(dialog); + if ("".equals(txtOldDir.getText())) + { + Lib.gui.showError(this, "Please choose a directory"); + return; + } + + if (jcbCreateUser.isSelected()) + { + if ("".equals(txtUser.getText())) + { + Lib.gui.showError(this, "Please enter a user name"); + return; + } + else if ("".equals(txtGroup.getText())) + { + Lib.gui.showError(this, "Please enter a group name"); + return; + } + worker.setOldUser (txtUser.getText()); + worker.setOldGroup (txtGroup.getText()); + } + + worker.setUserDialog(jcbUserDialog.isSelected()); worker.setOldDir(txtOldDir.getText()); dialog.run(worker); } @@ -104,7 +137,7 @@ { JPanel p = new JPanel(); - FlexLayout fl = new FlexLayout(3,1); + FlexLayout fl = new FlexLayout(3,5); fl.setColProp(1, FlexLayout.EXPAND); p.setLayout(fl); @@ -112,9 +145,22 @@ p.add("1,0,x", txtOldDir); p.add("2,0", btnBrowse); + p.add("0,1,1,1,3", jcbCreateUser); + p.add("0,2", new JLabel("User")); + p.add("1,2", txtUser); + p.add("0,3", new JLabel("Group")); + p.add("1,3", txtGroup); + + p.add("0,4,x,c,3", jcbUserDialog); + + txtUser.setEnabled(false); + txtGroup.setEnabled(false); + btnBrowse.addActionListener(this); btnBrowse.setActionCommand("browse"); + jcbCreateUser.addActionListener(this); + return p; } @@ -127,6 +173,12 @@ private JTextField txtOldDir = new JTextField(20); private JButton btnBrowse = new JButton("Browse"); private JFileChooser jfcBrowser = new JFileChooser(); + + private JCheckBox jcbCreateUser = new JCheckBox("Assign unowned metadata to this user:"); + private JTextField txtUser = new JTextField(20); + private JTextField txtGroup = new JTextField(20); + + private JCheckBox jcbUserDialog = new JCheckBox("Popup dialog to choose users"); } //============================================================================== Added: trunk/src/org/fao/gast/gui/panels/migration/oldinst/UserDialog.java =================================================================== --- trunk/src/org/fao/gast/gui/panels/migration/oldinst/UserDialog.java (rev 0) +++ trunk/src/org/fao/gast/gui/panels/migration/oldinst/UserDialog.java 2008-05-30 14:32:31 UTC (rev 1310) @@ -0,0 +1,189 @@ +//============================================================================== + +package org.fao.gast.gui.panels.migration.oldinst; + +import java.awt.Dialog; +import java.awt.Panel; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.ArrayList; +import java.util.List; +import javax.swing.ComboBoxModel; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JComboBox; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JList; +import javax.swing.JScrollPane; +import javax.swing.ListModel; +import javax.swing.event.ListDataListener; +import org.dlib.gui.FlexLayout; + +//============================================================================== + +public class UserDialog extends JDialog implements ActionListener +{ + //--------------------------------------------------------------------------- + //--- + //--- Constructor + //--- + //--------------------------------------------------------------------------- + + public UserDialog(Dialog d, List<Owner> candid, List<Owner> admins, List<Owner> groups) + { + super(d, "Choose user", true); + + this.candid = candid; + + for (Owner o : admins) + this.admins.add(new Entity(o.user, o.name)); + + for (Owner o : groups) + this.groups.add(new Entity(o.group, o.groupName)); + + selAdmin = this.admins.get(0); + selGroup = this.groups.get(0); + + Panel p = new Panel(); + + FlexLayout fl = new FlexLayout(2,6); + fl.setColProp(1, FlexLayout.EXPAND); + fl.setRowProp(1, FlexLayout.EXPAND); + + p.setLayout(fl); + p.add("0,0,x,c,2", new JLabel("Candidates found")); + p.add("0,1,x,x,2", new JScrollPane(jlCandid)); + p.add("0,2,x,c,2", chUseAdm); + p.add("0,3", new JLabel("User")); + p.add("1,3,x", cbAdmins); + p.add("0,4", new JLabel("Group")); + p.add("1,4,x", cbGroups); + p.add("1,5,c", btOk); + + btOk.addActionListener(this); + + getContentPane().add(p); + + jlCandid.setModel(candidModel); + cbAdmins.setModel(adminsModel); + cbGroups.setModel(groupsModel); + } + + //--------------------------------------------------------------------------- + //--- + //--- API methods + //--- + //--------------------------------------------------------------------------- + + public Owner run() + { + pack(); + setSize(400, 400); + setVisible(true); + + if (chUseAdm.isSelected()) + return new Owner(selAdmin.id, selGroup.id, ""); + + return (Owner) jlCandid.getSelectedValue(); + } + + //--------------------------------------------------------------------------- + + public void actionPerformed(ActionEvent e) + { + setVisible(false); + } + + //--------------------------------------------------------------------------- + //--- + //--- Variables + //--- + //--------------------------------------------------------------------------- + + private List<Owner> candid; + private List<Entity> admins = new ArrayList<Entity>(); + private List<Entity> groups = new ArrayList<Entity>(); + + //--------------------------------------------------------------------------- + + private ListModel candidModel = new ListModel() + { + public int getSize() { return candid.size(); } + + public Object getElementAt(int index) { return candid.get(index); } + + public void addListDataListener(ListDataListener l) {} + public void removeListDataListener(ListDataListener l){} + }; + + //--------------------------------------------------------------------------- + + private ComboBoxModel adminsModel = new ComboBoxModel() + { + + public void setSelectedItem(Object anItem) + { + selAdmin = (Entity) anItem; + } + + public Object getSelectedItem() { return selAdmin; } + + public int getSize() { return admins.size(); } + + public Object getElementAt(int index) { return admins.get(index); } + + public void addListDataListener(ListDataListener l) {} + public void removeListDataListener(ListDataListener l){} + }; + + //--------------------------------------------------------------------------- + + private ComboBoxModel groupsModel = new ComboBoxModel() + { + + public void setSelectedItem(Object anItem) + { + selGroup = (Entity) anItem; + } + + public Object getSelectedItem() { return selGroup; } + + public int getSize() { return groups.size(); } + + public Object getElementAt(int index) { return groups.get(index); } + + public void addListDataListener(ListDataListener l) {} + public void removeListDataListener(ListDataListener l){} + }; + + //--------------------------------------------------------------------------- + + private Entity selAdmin; + private Entity selGroup; + + private JList jlCandid = new JList(); + private JComboBox cbAdmins = new JComboBox(); + private JComboBox cbGroups = new JComboBox(); + private JCheckBox chUseAdm = new JCheckBox("Use one of the following user/group"); + private JButton btOk = new JButton("Ok"); +} + +//============================================================================== + +class Entity +{ + public String id; + public String name; + + public Entity(String id, String name) + { + this.id = id; + this.name = name; + } + + public String toString() { return name; } +} + +//============================================================================== + Modified: trunk/src/org/fao/gast/gui/panels/migration/oldinst/Worker.java =================================================================== --- trunk/src/org/fao/gast/gui/panels/migration/oldinst/Worker.java 2008-05-30 14:30:55 UTC (rev 1309) +++ trunk/src/org/fao/gast/gui/panels/migration/oldinst/Worker.java 2008-05-30 14:32:31 UTC (rev 1310) @@ -25,9 +25,13 @@ import java.sql.SQLException; import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; +import java.util.StringTokenizer; +import javax.swing.JOptionPane; import jeeves.resources.dbms.Dbms; import jeeves.utils.Util; import jeeves.utils.Xml; @@ -41,6 +45,7 @@ import org.fao.gast.lib.druid.ImportField; import org.fao.geonet.kernel.XmlSerializer; import org.jdom.Element; +import org.jdom.output.*; //============================================================================== @@ -69,6 +74,27 @@ } //--------------------------------------------------------------------------- + + public void setOldUser(String user) + { + oldUserName = user; + } + + //--------------------------------------------------------------------------- + + public void setOldGroup(String group) + { + oldGroupName = group; + } + + //--------------------------------------------------------------------------- + + public void setUserDialog(boolean yesno) + { + showDialog = yesno; + } + + //--------------------------------------------------------------------------- //--- //--- Migration process //--- @@ -77,7 +103,10 @@ public void run() { if (!openSource()) + { + dlg.stop(); return; + } Resource oldRes = null; Resource newRes = null; @@ -87,7 +116,7 @@ oldRes = source.config.createResource(); newRes = Lib.config.createResource(); - executeJob(oldRes, newRes); + executeJob((Dbms) oldRes.open(), (Dbms) newRes.open()); } catch(Throwable t) { @@ -126,21 +155,56 @@ //--------------------------------------------------------------------------- - private void executeJob(Resource oldRes, Resource newRes) throws Exception + private void checkOldUser(Dbms oldDbms) throws Exception { - Dbms newDbms = (Dbms) newRes.open(); - Dbms oldDbms = (Dbms) oldRes.open(); + String query = "SELECT id, profile FROM users WHERE username = ?"; + List oldUserIds = oldDbms.select(query, oldUserName).getChildren(); + oldDbms.commit(); - dlg.reset(9); - dlg.advance("Setting up new installation"); + // Get the user id and profile from the db + if (oldUserIds.size() == 0) + throw new Exception("Can't find user \"" + oldUserName + "\" in the old GeoNetwork"); - String tables[] = { "OperationAllowed", "UserGroups", "MetadataCateg", "Users", "Metadata", "GroupsDes", "Groups", "Categories", "CategoriesDes" }; - HashSet notTables = new HashSet(); - for (int i = 0;i < tables.length;i++) - notTables.add(tables[i]); + // Check if the user is an Editor + String profile = ((Element)oldUserIds.get(0)).getChildText("profile"); + if (!"Editor".equals(profile)) + throw new Exception("User \"" + oldUserName + "\" is not an Editor"); - Lib.database.setupNoFill(newRes,notTables,callBack); + // Check if the group exists + oldUserId = Integer.parseInt(((Element)oldUserIds.get(0)).getChildText("id")); + query = "SELECT id FROM groups WHERE name = ?"; + List oldGroupIds = oldDbms.select(query, oldGroupName).getChildren(); + oldDbms.commit(); + if (oldGroupIds.size() == 0) + throw new Exception("Can't find group \"" + oldGroupName + "\" in the old GeoNetwork"); + + // Check if the user belongs to the given group + oldGroupId = Integer.parseInt(((Element)oldGroupIds.get(0)).getChildText("id")); + + query = "SELECT groupId FROM userGroups WHERE groupId = ? AND userId = ?"; + List userGroups = oldDbms.select(query, new Integer(oldGroupId), new Integer(oldUserId)).getChildren(); + oldDbms.commit(); + if (userGroups.size() == 0) + throw new Exception("User \"" + oldUserName + "\" doesn't belong to group \"" + oldGroupName + "\""); + } + + //--------------------------------------------------------------------------- + + private void executeJob(Dbms oldDbms, Dbms newDbms) throws Exception + { + if (oldUserName == null) + dlg.reset(9); + else + { + dlg.reset(10); + dlg.advance("Checking old user and group"); + checkOldUser(oldDbms); + } + + dlg.advance("Removing data in new installation"); + removeAll(newDbms); + Set<String> langs = getLanguages(newDbms); dlg.advance("Migrating users"); @@ -168,10 +232,44 @@ restoreLocalizedRecords(newDbms); Lib.metadata.clearIndexes(); + + String message = "Migrated metadata : "+ mdIds.size() + "\n"+ + nNoEditor + " metadata had no editor and were assigned to " + oldUserName + ":" + oldGroupName + "\n"+ + nNoPriv + " metadata had no admin privileges and were assigned to " + oldUserName + ":" + oldGroupName + "\n"+ + nMatchedUsers + " users were successfully assigned from their surname\n"+ + nMultiUser + " metadata were assigned to first candidate\n"; + + Lib.gui.showInfo(dlg, message); + Lib.log.debug(message); } //--------------------------------------------------------------------------- + private void removeAll(Dbms newDbms) throws SQLException + { + //--- we commit at each step because an old migration could have failed and + //--- we could have plenty of records + + newDbms.execute("DELETE FROM OperationAllowed"); + newDbms.commit(); + newDbms.execute("DELETE FROM MetadataCateg"); + newDbms.commit(); + newDbms.execute("DELETE FROM UserGroups"); + newDbms.commit(); + newDbms.execute("DELETE FROM Metadata"); + newDbms.commit(); + newDbms.execute("DELETE FROM GroupsDes"); + newDbms.execute("DELETE FROM Groups"); + newDbms.commit(); + newDbms.execute("DELETE FROM Users"); + newDbms.commit(); + newDbms.execute("DELETE FROM CategoriesDes"); + newDbms.execute("DELETE FROM Categories"); + newDbms.commit(); + } + + //--------------------------------------------------------------------------- + private Set<String> getLanguages(Dbms newDbms) throws SQLException { List langs = newDbms.select("SELECT * FROM Languages").getChildren(); @@ -268,9 +366,14 @@ private void migrateMetadata(Dbms oldDbms, Dbms newDbms) throws Exception { + nNoPriv = 0; + nNoEditor = 0; + nMatchedUsers = 0; + nMultiUser = 0; + String query = "SELECT id, uuid, schemaId, isTemplate, createDate, "+ " lastChangeDate as changeDate, source "+ - "FROM Metadata"; + "FROM Metadata ORDER BY id"; List metadata = oldDbms.select(query).getChildren(); @@ -295,53 +398,408 @@ if (!oldSiteId.equals(source)) continue; - //--- calculate owner and group owner + //--- retrieve metadata - query = "SELECT groupId FROM OperationAllowed WHERE metadataId=? AND operationId=4"; - List privil = oldDbms.select(query, new Integer(id)).getChildren(); + md.getChild("source").setText(newSiteId); + + Element xml = XmlSerializer.select(oldDbms, "Metadata", id); oldDbms.commit(); - if (privil.size() == 0) - throw new Exception("Metadata has no admin privilege --> id:"+id); + //--- Identify the best owner - String groupOwner = ((Element) privil.get(0)).getChildText("groupid"); + List<Owner> owners = getPossibleOwners(oldDbms, id, xml); - query = "SELECT DISTINCT id FROM Users, UserGroups WHERE id=userId AND profile='Editor' AND groupId=?"; - List usrGrps = oldDbms.select(query, new Integer(groupOwner)).getChildren(); - oldDbms.commit(); + Owner owner = null; - if (usrGrps.size() == 0) - throw new Exception("No editor for metadata --> id:"+id); + //--- only 1 possible owner -> assign it - String owner = ((Element) usrGrps.get(0)).getChildText("id"); + if (owners.size() == 1) + owner = owners.get(0); - //--- no, ok we can store it + //--- more possible owners -> try to match the best one - md.getChild("source").setText(newSiteId); + else if (owners.size() > 1) + { + if (showDialog) + owner = promptUser(oldDbms, id, owners); + else + { + Lib.log.debug("Assigning metadata id " + id + " to first candidate found: "+ owners.get(0) + + " surname: " + owners.get(0).surname); + owner = owners.get(0); + nMultiUser++; + } + } - Element xml = XmlSerializer.select(oldDbms, "Metadata", id); - oldDbms.commit(); + if (owner.group==null) { + Lib.log.info("No group found. Using "+ oldGroupName +" (" + oldGroupId + ")"); + owner.group = "" + oldGroupId; + owner.groupName = oldGroupName; + } + //--- no, ok we can store it + md.addContent(new Element("root") .setText(xml.getName())); md.addContent(new Element("isHarvested").setText("n")); md.addContent(new Element("data") .setText(Xml.getString(xml))); - md.addContent(new Element("owner") .setText(owner)); - md.addContent(new Element("groupOwner") .setText(groupOwner)); + md.addContent(new Element("owner") .setText(owner.user)); + md.addContent(new Element("groupOwner") .setText(owner.group)); Lib.database.insert(newDbms, "Metadata", md, idMapper); - //--- we can have even milions of records so it is convenient to commit + //--- we can have even millions of records so it is convenient to commit //--- at each insert newDbms.commit(); + Lib.log.info("Metadata (id: " + id + ") successfully assigned to "+ owner.toString()); + mdIds.add(id); } + } - System.out.println("Migrated metadata : "+ mdIds.size()); + //--------------------------------------------------------------------------- + + private List<Owner> getPossibleOwners(Dbms dbms, String id, Element md) throws Exception + { + List<Owner> owners = new ArrayList<Owner>(); // The candidate owners that should be returned + List<Owner> ownersPriv = new ArrayList<Owner>(); // Editors and above in groups that have edit or admin privileges on the record + String group = null; + + /* + * Select the best new owner of the metadata record + * + * Select the metadata author based on the metadata + * Select the group(s) that has/have edit or admin privileges to it + * Select all users from the above selected group(s) that are not RegisteredUsers + * + * Is the identified author part of the group that has edit or admin privileges to the record? + * Yes: assign that author as the new owner + * No: + * - allow the user to select the new owner, or + * - automatically select the first Editor, otherwise the first UserAdmin and finally the first Administrator + * If there are no groups with edit or admin privileges, assign the record to the migration user/group + * + * */ + + Lib.log.debug("Processing metadata with id: "+ id); + + Owner author = getUserFromMetadata(dbms, md); //--- try to match the author of the metadata + + // Select the groups that have admin rights on this metadata + String query = "SELECT groupId FROM OperationAllowed WHERE metadataId=? AND operationId=4 AND groupId<>2"; + List privil = dbms.select(query, new Integer(id)).getChildren(); + dbms.commit(); + + if (privil.size()==0){ + query = "SELECT groupId FROM OperationAllowed WHERE metadataId=? AND operationId=2 AND groupId<>2"; + privil = dbms.select(query, new Integer(id)).getChildren(); + dbms.commit(); + } + Lib.log.debug(privil.size() + " group(s) found with 'edit' or 'admin' privilege"); + + if (privil.size() > 0) { + //--- create a list with users that have enough privileges + for (Object grCh : privil) + { + // find a suitable owner for the record (someone with editor, useradmin or administrator role) + group = ((Element) grCh).getChildText("groupid"); + + Lib.log.debug("Query for group: "+ group); + + query = "SELECT DISTINCT id, name, surname, profile FROM Users, UserGroups WHERE id=userId AND profile<>'RegisteredUser' AND groupId=?"; + + List usrGrps = dbms.select(query, new Integer(group)).getChildren(); + dbms.commit(); + + for (Object o : usrGrps) + { + Element oGroup = (Element) o; + + Owner oT = new Owner(oGroup.getChildText("id"),group,oGroup.getChildText("profile"), oGroup.getChildText("name"), oGroup.getChildText("surname"),""); + oT.surname = ((Element) o).getChildText("surname"); + ownersPriv.add(oT); + Lib.log.debug(oT.toString() + " added as possible owner."); + } + } + } + + // Metadata author(s) and privileges found + if (ownersPriv!=null && author!=null) { + if (author.user != null) + { + // Loop through all users that are member of the group(s) of the record + for (Owner owner : ownersPriv) { + if (owner.user.equals(author.user)) { + // We found the owner! Assign and leave + Lib.log.debug("Metadata (id: " + id + ") will be assigned to user: "+ author.user); + + nMatchedUsers++; + owners = new ArrayList<Owner>(); + owners.add(author); + return owners; + } + else if (author.profile.equals("Administrator")) { + // The metadata author is an administrator of the site, add it and leave + nMatchedUsers++; + owners = new ArrayList<Owner>(); + owners.add(new Owner(owner.user, owner.group, "Administrator")); + return owners; + } + } + // A candidate was not found in the group(s), log this and proceed + Lib.log.info("Metadata contact (user id: "+ author.user +") was not matched with candidates list"); + Lib.log.debug("Candidate list is:"); + for(Owner o : ownersPriv) + Lib.log.debug(" --> user: " + o.surname + " (" + o.user + + "), group: "+ o.groupName + " (" + o.group + ")"); + } + } + + // Privileges found, but no potential author + if (ownersPriv!=null) { + // Select the first editor + // Loop through all users that are member of the group(s) of the record + for (Owner owner : ownersPriv) { + if (owner.profile.equals("Editor")) { + // We found the first Editor! Assign and leave + + Lib.log.info("Metadata " + id + + " will be assigned to user " + owner.surname + + " ("+ owner.user +") and group " + owner.groupName + + " ("+ owner.group+")"); + + nMatchedUsers++; + owners = new ArrayList<Owner>(); + owners.add(new Owner(owner.user, owner.group, owner.profile, owner.name, owner.surname, owner.groupName)); + return owners; + } + } + // No editor found, try for the first UserAdmin + for (Owner owner : ownersPriv) { + if (owner.profile.equals("UserAdmin")) { + // We found the first UserAdmin! Assign and leave + + Lib.log.info("Metadata " + id + + " will be assigned to user " + owner.surname + + " ("+ owner.user +") and group " + owner.groupName + + " ("+ owner.group+")"); + + nMatchedUsers++; + owners = new ArrayList<Owner>(); + owners.add(new Owner(owner.user, owner.group, owner.profile, owner.name, owner.surname, owner.groupName)); + return owners; + } + } + // No editor found, try for the first UserAdmin + for (Owner owner : ownersPriv) { + if (owner.profile.equals("Administrator")) { + // We found the first UserAdmin! Assign and leave + + Lib.log.info("Metadata " + id + + " will be assigned to user " + owner.surname + + " ("+ owner.user +") and group " + owner.groupName + + " ("+ owner.group+")"); + + nMatchedUsers++; + owners = new ArrayList<Owner>(); + owners.add(new Owner(owner.user, owner.group, owner.profile, owner.name, owner.surname, owner.groupName)); + return owners; + } + } + } + + // Metadata author found, but no privileges + if (author!=null) { + // No editor found, try for the first UserAdmin + + if (author.user != null) + { + + Lib.log.info("Metadata " + id + + " will be assigned to user " + author.user + + ", group " + author.group + + ", profile "+ author.profile); + + nMatchedUsers++; + owners = new ArrayList<Owner>(); + owners.add(new Owner(author.user, author.group, author.profile)); + return owners; + } + } + + return assignDefaultUser(id, owners); } + //--------------------------------------------------------------------------- + + private List<Owner> assignDefaultUser(String id, List<Owner> owners) throws Exception + { + if (oldUserName == null) + { + // throw an Exception if default user and group were not given + throw new Exception("No candidate owner found for metadata with id: "+id); + } else { + // Assign default user and group if metadata is not owned + Lib.log.debug("Metadata with id ("+ id +") has no group with admin or edit privileges. Using user/group provided by GAST)"); + owners.add(new Owner("" + oldUserId, "" + oldGroupId, "Editor", "", oldUserName, oldGroupName)); + nNoPriv++; + return owners; + } + } + //--------------------------------------------------------------------------- + + private Owner getUserFromMetadata(Dbms dbms, Element md) throws Exception + { + //--- extract metadata author's surname + String surname = getSurname(md); + List<Owner> owners = new ArrayList<Owner>(); + Owner owner = new Owner(); + + if (surname == null) { + return null; + } + + String query = "SELECT id, name, surname, profile FROM Users WHERE surname=? AND profile<>'RegisteredUser'"; + List users = dbms.select(query, surname).getChildren(); + dbms.commit(); + + if (users.size()>0) { + // return (first) user + Element user = (Element) users.get(0); + + owner.user = user.getChildText("id"); + owner.name = user.getChildText("name"); + owner.surname = user.getChildText("surname"); + owner.profile = user.getChildText("profile"); + Lib.log.debug("Author: " + owner.name + " (" + owner.user + ")"); + String queryGroups = "SELECT DISTINCT userId, groupId, name FROM UserGroups, Groups WHERE groupId=id AND userId=?"; + List groups = dbms.select(queryGroups, user.getChildText("id")).getChildren(); + + dbms.commit(); + if (groups.size()!=0 && groups!=null) { + // Assign to first group + Element group= (Element) groups.get(0); + owner.group = group.getChildText("groupid"); + owner.groupName = group.getChildText("name"); + if (groups.size()>1) { + Lib.log.debug("Other groups:"); + for (Object gr : groups){ + group = (Element) gr; + Lib.log.debug(" - " + group.getChildText("name") + "(" + group.getChildText("groupid") + ")"); + } + } + + if (owner.group==null) Lib.log.debug(owner.name + " has no valid groups associated"); + } + Lib.log.debug("Author found: " + owner.toString()); + return owner; + } + else + Lib.log.debug("No user found for surname: "+ surname); + return null; + } + + //--------------------------------------------------------------------------- + + private Owner promptUser(Dbms dbms, String id, List<Owner> owners) throws SQLException + { + for (Owner owner : owners) + { + List list = dbms.select("SELECT surname, name FROM Users WHERE id = ?", new Integer(owner.user)).getChildren(); + + Element rec = (Element) list.get(0); + + owner.surname = rec.getChildText("surname"); + owner.name = rec.getChildText("name"); + + list = dbms.select("SELECT name FROM Groups WHERE id = ?", new Integer(owner.group)).getChildren(); + + rec = (Element) list.get(0); + + owner.groupName = rec.getChildText("name"); + } + + setupAdmins(dbms); + + UserDialog ud = new UserDialog(dlg, owners, admins, groups); + + Owner owner = ud.run(); + + Lib.log.debug("For metadata with id ("+id+ ") selected owner with dialog : "+owner); + + return owner; + } + + //--------------------------------------------------------------------------- + + private void setupAdmins(Dbms dbms) throws SQLException + { + if (admins != null) + return; + + admins = new ArrayList<Owner>(); + + List list = dbms.select("SELECT * FROM Users ORDER BY profile, surname").getChildren(); + + for (Object o : list) + { + Element rec = (Element) o; + + Owner owner = new Owner(); + owner.user = rec.getChildText("id"); + owner.name = rec.getChildText("surname") +" "+ rec.getChildText("name") +" ("+rec.getChildText("profile") +")"; + + admins.add(owner); + } + + //--- setup groups + + groups = new ArrayList<Owner>(); + + list = dbms.select("SELECT * FROM Groups ORDER BY name").getChildren(); + + for (Object o : list) + { + Element rec = (Element) o; + + Owner owner = new Owner(); + owner.group = rec.getChildText("id"); + owner.groupName= rec.getChildText("name"); + + groups.add(owner); + } + } + + //--------------------------------------------------------------------------- + + private String getSurname(Element md) + { + md = md.getChild("mdContact"); + + if (md == null) + return null; + + String author = md.getChildText("rpIndName"); + + if (author == null) + return null; + + if (author.trim().length() == 0) + return null; + + StringTokenizer st = new StringTokenizer(author.trim(), " "); + + String surname = st.nextToken(); + + if (st.hasMoreTokens()) + surname = st.nextToken(); + + return surname; + } + + //--------------------------------------------------------------------------- /** Migrates OperationAllowed */ private void migrateOperationAllowed(Dbms oldDbms, Dbms newDbms) throws SQLException @@ -362,7 +820,7 @@ Lib.database.insert(newDbms, "OperationAllowed", opAll, relationMapper); - //--- we can have even milions of records so it is convenient to commit + //--- we can have even millions of records so it is convenient to commit //--- at each insert newDbms.commit(); @@ -529,28 +987,23 @@ private GNSource source; private Set<String> mdIds = new HashSet<String>(); + private String oldUserName; + private String oldGroupName; + private int oldUserId; + private int oldGroupId; + + private int nNoPriv; + private int nNoEditor; + private int nMatchedUsers; + private int nMultiUser; + private ProgressDialog dlg; - //--------------------------------------------------------------------------- - - private DatabaseLib.CallBack callBack = new DatabaseLib.CallBack () - { - public void schemaObjects(int count) {} - public void removed(String object, String type) { - System.out.println("Removing : "+ object); - } - public void cyclicRefs(List<String> objects) { - System.out.println("Cyclic reference found: "+objects); - } - public void creating(String object, String type) { - System.out.println("Creating : "+ object); - } - public void skipping(String table) { - System.out.println("Skipping : "+ table); - } - public void filling (String table, String file) {} - }; + private boolean showDialog; + public List<Owner> admins; + public List<Owner> groups; + //--------------------------------------------------------------------------- private DatabaseLib.Mapper idMapper = new DatabaseLib.Mapper() @@ -655,3 +1108,43 @@ //============================================================================== +class Owner +{ + public String user; + public String group; + public String profile; + + public String surname; + public String name; + public String groupName; + + public Owner() {} + + public Owner(String user, String group, String profile) + { + this.user = user; + this.group= group; + this.profile= profile; + } + + public Owner(String user, String group, String profile, String name, String surname, String groupName) + { + this.user = user; + this.group= group; + this.profile= profile; + this.name= name; + this.surname= surname; + this.groupName= groupName; + } + + public String toString() + { + if (name != null) + return name +" "+ surname +"("+user+"), group: "+ groupName +" ("+group+"), profile: "+ profile+""; + + return "user:"+ user +", group:"+ group +", profile:" + profile; + } +} + + +//============================================================================== This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |