Hi Jesus,

Thanks for contributing your code to DSpace.  Please could you submit 
your patch to the DSpace Patch Tracker:

https://sourceforge.net/tracker/?group_id=19984&atid=319984

If you could also add information to the system documentation in the 
relevant place to explain how to use your changes that would be good.

There are some contribution guidelines available on the wiki, which will 
be of help in preparing a final patch:

http://wiki.dspace.org/index.php/ContributionGuidelines

Thanks a lot,

Richard

> I made a few interesting modifications in DSpace code, in particular in 
> the ItemImport.java:
> 
> When our DSpace implementation import metadata of other repositories it 
> checks that the importing documents are not inside. If this metadata has 
> changed then it overwrites the metadata stored of this document. Anyway, 
> the handle of our repository doesn't change. To find this metadata in 
> the repository I used the "original" handle of the document, which is 
> normally under the field "dc.identifier.citation" (it's possible that 
> there are more than one, in this case I get the first that includes 
> "http://hdl.handle.net";). There are few functions changed of the class 
> ItemImport.java, and uses an external class of my own creation 
> (Utilities.java).
> 
> The other change in the ItemImport.java it's that checks if the 
> collections, communities or repository has increased their numbers of 
> publications after an importing task. If this number it's greater than 
> 100 the class sends an email to the DSpace administrators. This number 
> can be easily modificable.
> 
> In the attachments there are a patch for the ItemImport.java and the 
> Utilities.java.
> 
> I hope that this changes are useful for DSpace users.
> 
> Greetings
> 
> 
> ------------------------------------------------------------------------
> 
> --- ItemImport.java.20070327  2007-04-13 12:16:53.000000000 +0200
> +++ ItemImport.java   2007-04-25 10:27:25.000000000 +0200
> @@ -60,6 +60,9 @@
>  import javax.xml.parsers.ParserConfigurationException;
>  import javax.xml.transform.TransformerException;
>  
> +import org.dspace.app.webui.util.I18nUtil;
> +import org.dspace.core.Email;
> +
>  import org.apache.commons.cli.CommandLine;
>  import org.apache.commons.cli.CommandLineParser;
>  import org.apache.commons.cli.HelpFormatter;
> @@ -88,6 +91,8 @@
>  import org.w3c.dom.NodeList;
>  import org.xml.sax.SAXException;
>  
> +import recercat.Utilities;
> +
>  /**
>   * Import items into DSpace. The conventional use is upload files by copying
>   * them. DSpace writes the item's bitstreams into its assetstore. Metadata is
> @@ -317,7 +322,6 @@
>          }
>  
>          ItemImport myloader = new ItemImport();
> -
>          // create a context
>          Context c = new Context();
>  
> @@ -400,6 +404,16 @@
>  
>          try
>          {
> +     //modified by JM: if the collections or communities grow 100 items or 
> more sends an e-mail
> +     //before begin the item importation count the items in each community 
> and collection
> +     int[] ndocsbeforecols, ndocsbeforecoms = null;
> +     ndocsbeforecols=new int[mycollections.length];
> +     ndocsbeforecoms=new int[mycollections.length];
> +     int 
> totalcountbefore=org.dspace.workflow.WorkflowManager.countAllItems(c);
> +     for (int q=0;q<mycollections.length;q++){ 
> +             ndocsbeforecols[q]=mycollections[q].countItems();
> +             
> ndocsbeforecoms[q]=mycollections[q].getCommunities()[0].countItems();
> +     }
>              c.setIgnoreAuthorization(true);
>  
>              if (command.equals("add"))
> @@ -415,8 +429,58 @@
>                  myloader.deleteItems(c, mapfile);
>              }
>  
> -            // complete all transactions
> -            c.complete();
> +     //and now count the items of the collections and communities
> +             String collec="";
> +             String comun="";
> +             String comofcol="";
> +             int countitems=0;
> +             String numdocscol="";
> +             String numdocscom="";
> +             for (int now=0;now<ndocsbeforecols.length;now++){
> +                     
> countitems=(mycollections[now].countItems()/100)-(ndocsbeforecols[now]/100);
> +                     if (countitems>0){ 
> collec+=mycollections[now].getMetadata("name")+" 
> ";numdocscol+=mycollections[now].countItems()+" ";}
> +                     
> countitems=(mycollections[now].getCommunities()[0].countItems()/100)-(ndocsbeforecoms[now]/100);
> +                     String 
> namecom=mycollections[now].getCommunities()[0].getMetadata("name")+" ";
> +                     if (countitems>0){ 
> comun+=namecom;numdocscom+=mycollections[now].getCommunities()[0].countItems()+"
>  ";}
> +                     comofcol+=namecom;
> +             }
> +             //modified by JM : now we send an email
> +             Email email;
> +             if (collec.length()>0)
> +             { 
> +             email = 
> ConfigurationManager.getEmail(I18nUtil.getEmailFilename("ca","notifica_importacio_coleccions"));
> +             email.addRecipient("[EMAIL PROTECTED]");
> +             email.addArgument(collec);
> +             email.addArgument(comofcol);
> +             email.addArgument(numdocscol);
> +             email.send();
> +             }
> +                if (comun.length()>0)
> +                {
> +             email = 
> ConfigurationManager.getEmail(I18nUtil.getEmailFilename("ca","notifica_importacio_comunitats"));
> +                email.addRecipient("[EMAIL PROTECTED]");
> +                email.addArgument(comun);
> +             email.addArgument(numdocscom);
> +                email.send();
> +                }
> +             int totalcountafter= 
> org.dspace.workflow.WorkflowManager.countAllItems(c);
> +             if ((totalcountafter/1000)-(totalcountbefore/1000)>0){
> +                     email = 
> ConfigurationManager.getEmail(I18nUtil.getEmailFilename("ca","notifica_importacio_repositori"));
> +                     email.addRecipient("[EMAIL PROTECTED]");
> +                     email.addArgument(comofcol);
> +                     email.addArgument(new Integer(totalcountafter));
> +                     email.send();
> +             }
> +
> +             // complete all transactions
> +             c.complete();
> +
>          }
>          catch (Exception e)
>          {
> @@ -521,28 +585,24 @@
>          // and re-assign the old handle
>          Iterator i = myhash.keySet().iterator();
>          ArrayList itemsToDelete = new ArrayList();
> -
>          while (i.hasNext())
>          {
>              // get the old handle
>              String newItemName = (String) i.next();
>              String oldHandle = (String) myhash.get(newItemName);
> -
>              Item oldItem = null;
>              Item newItem = null;
> -
>              if (oldHandle.indexOf('/') != -1)
>              {
>                  System.out.println("\tReplacing:  " + oldHandle);
> -
>                  // add new item, locate old one
> +             HandleManager.resolveToObject(c,oldHandle);
>                  oldItem = (Item) HandleManager.resolveToObject(c, oldHandle);
>              }
>              else
>              {
>                  oldItem = Item.find(c, Integer.parseInt(oldHandle));
>              }
> -            
>              /* Rather than exposing public item methods to change handles -- 
>               * two handles can't exist at the same time due to key 
> constraints
>               * so would require temp handle being stored, old being copied 
> to new and
> @@ -560,12 +620,10 @@
>              {
>                  throw new Exception("can't open handle file: " + 
> handleFile.getCanonicalPath());
>              }
> -            
>              handleOut.println(oldHandle);
>              handleOut.close();
>              
>              deleteItem(c, oldItem);
> -            
>              newItem = addItem(c, mycollections, sourceDir, newItemName, 
> null);
>          }
>      }
> @@ -623,9 +681,9 @@
>          }
>  
>          // now fill out dublin core for item
> -        loadMetadata(c, myitem, path + File.separatorChar + itemname
> -                + File.separatorChar);
> -
> +     //modified: return the first handle original of the document, where it 
> was inserted the first time
> +     //if this handle exists it is in the field metadata 
> dc.identifier.citation 
> +        String citationfound=loadMetadata(c, myitem, path + 
> File.separatorChar + itemname  + File.separatorChar);
>          // and the bitstreams from the contents file
>          // process contents file, add bistreams and bundles
>          processContentsFile(c, myitem, path + File.separatorChar + itemname,
> @@ -646,8 +704,14 @@
>          else
>          {
>              // only process handle file if not using workflow system
> -            String myhandle = processHandleFile(c, myitem, path
> -                    + File.separatorChar + itemname, "handle");
> +            String myhandle = processHandleFile(c, myitem, path + 
> File.separatorChar + itemname, "handle");
> +             //modified: if the original handle (citationfound) it's in the 
> repository, then we have to replace
> +             //but with the same internal handle
> +             Item olditem=null;
> +                if (citationfound!=null){
> +                        olditem= 
> Utilities.findOriginalHandle(mycollections[0], citationfound);
> +                     if (olditem!=null){deleteItem(c, 
> olditem);myhandle=olditem.getHandle();}
> +                }
>  
>              // put item in system
>              if (!isTest)
> @@ -660,7 +724,6 @@
>                  mapOutput = itemname + " " + myhandle;
>              }
>          }
> -
>          // now add to multiple collections if requested
>          if (mycollections.length > 1)
>          {
> @@ -678,9 +741,7 @@
>          {
>              mapOut.println(mapOutput);
>          }
> -
> -        c.commit();
> -
> +     c.commit();
>          return myitem;
>      }
>  
> @@ -772,23 +833,25 @@
>      }
>  
>      // Load all metadata schemas into the item.
> -    private void loadMetadata(Context c, Item myitem, String path)
> +   //modified: return the dc.identifier.citation
> +    private String loadMetadata(Context c, Item myitem, String path)
>              throws SQLException, IOException, ParserConfigurationException,
>              SAXException, TransformerException
>      {
>          // Load the dublin core metadata
> -        loadDublinCore(c, myitem, path + "dublin_core.xml");
> -
> +        String citationfound=loadDublinCore(c, myitem, path + 
> "dublin_core.xml");
>          // Load any additional metadata schemas
>          File folder = new File(path);
>          File file[] = folder.listFiles(metadataFileFilter);
>          for (int i = 0; i < file.length; i++)
>          {
> -            loadDublinCore(c, myitem, file[i].getAbsolutePath());
> +            citationfound+=loadDublinCore(c, myitem, 
> file[i].getAbsolutePath());
>          }
> +     return citationfound;
>      }
>  
> -    private void loadDublinCore(Context c, Item myitem, String filename)
> +     //modified: return the dc.identifier.citation
> +    private String loadDublinCore(Context c, Item myitem, String filename)
>              throws SQLException, IOException, ParserConfigurationException,
>              SAXException, TransformerException //, AuthorizeException
>      {
> @@ -815,16 +878,19 @@
>                  "/dublin_core/dcvalue");
>  
>          System.out.println("\tLoading dublin core from " + filename);
> -
> +     String citationfound="";
>          // Add each one as a new format to the registry
>          for (int i = 0; i < dcNodes.getLength(); i++)
>          {
>              Node n = dcNodes.item(i);
> -            addDCValue(myitem, schema, n);
> +            citationfound+=addDCValue(myitem, schema, n);
>          }
> +     return citationfound;
>      }
>  
> -    private void addDCValue(Item i, String schema, Node n) throws 
> TransformerException
> +//modified: return the dc.identifier.citation
> +//checks if exists the dc.identifier.citation
> +    private String addDCValue(Item i, String schema, Node n) throws 
> TransformerException
>      {
>          String value = getStringValue(n); //n.getNodeValue();
>          // compensate for empty value getting read as "null", which won't 
> display
> @@ -836,6 +902,8 @@
>          // //getElementData(n,
>          // "qualifier");
>          String language = getAttributeValue(n, "language");
> +     String citationfound="";
> +     if (element.equals("identifier") && qualifier.equals("citation") && 
> value.startsWith("http")) citationfound=value;
>  
>          System.out.println("\tSchema: " + schema + " Element: " + element + 
> " Qualifier: " + qualifier
>                  + " Value: " + value);
> @@ -861,6 +929,7 @@
>          {
>              i.addMetadata(schema, element, qualifier, language, value);
>          }
> +     return citationfound;
>      }
>  
>      /**
> 
> 
> ------------------------------------------------------------------------
> 
> package recercat;
> 
> import org.dspace.handle.HandleManager;
> import org.dspace.core.Context;
> import org.dspace.content.Item;
> import org.dspace.content.Collection;
> import org.dspace.eperson.EPerson;
> import org.dspace.content.ItemIterator;
> import org.dspace.content.DCValue;
> public class Utilities{
>       public Utilities(){}
> 
>       /* This function finds an original handle of a import document of 
> another repository. The Metadata associated is dc.identifier.citation
>       * col is the document's collection
>       * originalhandle is the original handle of the document
>       * if the item it's found, then it's returned
>       */
>       public static Item findOriginalHandle(Collection col, String 
> originalhandle){
>               try{
>                       Context c= new Context();
>                       ItemIterator it = col.getItems();
>                       boolean found=false;
>                       Item ite=null;
>                       while (it.hasNext()&& (found==false)){
>                               ite= it.next();
>                               DCValue[] dcv= 
> ite.getDC("identifier",Item.ANY,Item.ANY);
>                               for (int k=0;k<dcv.length;k++)
>                               {
>                               if (dcv[k].qualifier.equals("citation")&& 
> dcv[k].value.equals(originalhandle))
>                               found=true;
>                               }
>                       }
>                       if (found==true) return ite;
>                       else return null;
>               } catch (Exception e){
>                       e.printStackTrace();
>                       return null;
>               }
>       }
> 
> }
> 
> 
> ------------------------------------------------------------------------
> 
> -------------------------------------------------------------------------
> This SF.net email is sponsored by DB2 Express
> Download DB2 Express C - the FREE version of DB2 express and take
> control of your XML. No limits. Just data. Click to get it now.
> http://sourceforge.net/powerbar/db2/
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> DSpace-tech mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/dspace-tech


-- 
Richard
------------------------------------------------------------------------
Richard Jones            | t: +44 (0)20 759 [48614 / 41815]
Web & Database           | e: [EMAIL PROTECTED]
   Technology Specialist  | b: http://chronicles-of-richard.blogspot.com/
Imperial College London  |
------------------------------------------------------------------------

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
DSpace-tech mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/dspace-tech

Reply via email to