Package org.dspace.content

Examples of org.dspace.content.Item$MetadataCache


             *  to the same names they had in the Zip, since those MUST
             *  match the URL references in <Flocat> and <mdRef> elements.
             */
            METSManifest manifest = null;
            wi = WorkspaceItem.create(context, collection, useTemplate);
            Item item = wi.getItem();
            Bundle contentBundle = null;
            Bundle mdBundle = null;
            ZipEntry ze;
            while ((ze = zip.getNextEntry()) != null)
            {
                if (ze.isDirectory())
                    continue;
                Bitstream bs = null;
                String fname = ze.getName();
                if (fname.equals(MANIFEST_FILE))
                {
                    if (preserveManifest)
                    {
                        mdBundle = item.createBundle(Constants.METADATA_BUNDLE_NAME);
                        bs = mdBundle.createBitstream(new PackageUtils.UnclosableInputStream(zip));
                        bs.setName(fname);
                        bs.setSource(fname);

                        // Get magic bitstream format to identify manifest.
                        BitstreamFormat manifestFormat = null;
                        manifestFormat = PackageUtils.findOrCreateBitstreamFormat(context,
                             MANIFEST_BITSTREAM_FORMAT, "application/xml",
                             MANIFEST_BITSTREAM_FORMAT+" package manifest");
                        bs.setFormat(manifestFormat);

                        manifest = METSManifest.create(bs.retrieve(), validate);
                    }
                    else
                    {
                        manifest = METSManifest.create(new PackageUtils.UnclosableInputStream(zip), validate);
                        continue;
                    }
                }
                else
                {
                  // we need to create the bundle only the first time
                  if (contentBundle == null)
                  {
                    contentBundle = item.createBundle(Constants.CONTENT_BUNDLE_NAME);
                  }
                    bs = contentBundle.createBitstream(new PackageUtils.UnclosableInputStream(zip));
                    bs.setSource(fname);
                    bs.setName(fname);
                }
                packageFiles.add(fname);
                bs.setSource(fname);
                bs.update();
            }
            zip.close();

            if (manifest == null)
                throw new PackageValidationException("No METS Manifest found (filename="+MANIFEST_FILE+").  Package is unacceptable.");

            // initial sanity checks on manifest (in subclass)
            checkManifest(manifest);

            /* 2. Grovel a file list out of METS Manifest and compare
             *  it to the files in package, as an integrity test.
             */
            List manifestContentFiles = manifest.getContentFiles();

            // Compare manifest files with the ones found in package:
            //  a. Start with content files (mentioned in <fileGrp>s)
            HashSet missingFiles = new HashSet();
            for (Iterator mi = manifestContentFiles.iterator(); mi.hasNext(); )
            {
                // First locate corresponding Bitstream and make
                // map of Bitstream to <file> ID.
                Element mfile = (Element)mi.next();
                String mfileId = mfile.getAttributeValue("ID");
                if (mfileId == null)
                    throw new PackageValidationException("Invalid METS Manifest: file element without ID attribute.");
                String path = METSManifest.getFileName(mfile);
                Bitstream bs = contentBundle.getBitstreamByName(path);
                if (bs == null)
                {
                    log.warn("Cannot find bitstream for filename=\""+path+
                             "\", skipping it..may cause problems later.");
                    missingFiles.add(path);
                }
                else
                {
                    fileIdToBitstream.put(mfileId, bs);

                    // Now that we're done using Name to match to <file>,
                    // set default bitstream Name to last path element;
                    // Zip entries all have '/' pathname separators
                    // NOTE: set default here, hopefully crosswalk of
                    // a bitstream techMD section will override it.
                    String fname = bs.getName();
                    int lastSlash = fname.lastIndexOf('/');
                    if (lastSlash >= && lastSlash+1 < fname.length())
                        bs.setName(fname.substring(lastSlash+1));

                    // Set Default bitstream format:
                    //  1. attempt to guess from MIME type
                    //  2. if that fails, guess from "name" extension.
                    String mimeType = mfile.getAttributeValue("MIMETYPE");
                    BitstreamFormat bf = (mimeType == null) ? null :
                            BitstreamFormat.findByMIMEType(context, mimeType);
                    if (bf == null)
                        bf = FormatIdentifier.guessFormat(context, bs);
                    bs.setFormat(bf);

                    // if this bitstream belongs in another Bundle, move it:
                    String bundleName = manifest.getBundleName(mfile);
                    if (!bundleName.equals(Constants.CONTENT_BUNDLE_NAME))
                    {
                        Bundle bn;
                        Bundle bns[] = item.getBundles(bundleName);
                        if (bns != null && bns.length > 0)
                            bn = bns[0];
                        else
                            bn = item.createBundle(bundleName);
                        bn.addBitstream(bs);
                        contentBundle.removeBitstream(bs);
                    }

                    // finally, build compare lists by deleting matches.
                    if (packageFiles.contains(path))
                        packageFiles.remove(path);
                    else
                        missingFiles.add(path);
                }
            }

            //  b. Process files mentioned in <mdRef>s - check and move
            //     to METADATA bundle.
            for (Iterator mi = manifest.getMdFiles().iterator(); mi.hasNext(); )
            {
                Element mdref = (Element)mi.next();
                String path = METSManifest.getFileName(mdref);

                // finally, build compare lists by deleting matches.
                if (packageFiles.contains(path))
                    packageFiles.remove(path);
                else
                    missingFiles.add(path);

                // if there is a bitstream with that name in Content, move
                // it to the Metadata bundle:
                Bitstream mdbs = contentBundle.getBitstreamByName(path);
                if (mdbs != null)
                {
                    if (mdBundle == null)
                        mdBundle = item.createBundle(Constants.METADATA_BUNDLE_NAME);
                    mdBundle.addBitstream(mdbs);
                    contentBundle.removeBitstream(mdbs);
                }
            }

            // KLUDGE: make sure Manifest file doesn't get flagged as missing
            // or extra, since it won't be mentioned in the manifest.
            if (packageFiles.contains(MANIFEST_FILE))
                packageFiles.remove(MANIFEST_FILE);

            // Give subclass a chance to refine the lists of in-package
            // and missing files, delete extraneous files, etc.
            checkPackageFiles(packageFiles, missingFiles, manifest);

            // Any discrepency in file lists is a fatal error:
            if (!(packageFiles.isEmpty() && missingFiles.isEmpty()))
            {
                StringBuffer msg = new StringBuffer("Package is unacceptable: contents do not match manifest.");
                if (!missingFiles.isEmpty())
                {
                    msg.append("\nPackage is missing these files listed in Manifest:");
                    for (Iterator mi = missingFiles.iterator(); mi.hasNext(); )
                        msg.append("\n\t"+(String)mi.next());
                }
                if (!packageFiles.isEmpty())
                {
                    msg.append("\nPackage contains extra files NOT in manifest:");
                    for (Iterator mi = packageFiles.iterator(); mi.hasNext(); )
                        msg.append("\n\t"+(String)mi.next());
                }
                throw new PackageValidationException(msg.toString());
            }

            /* 3. crosswalk the metadata
             */
            // get mdref'd streams from "callback" object.
            MdrefManager callback = new MdrefManager(mdBundle);

            chooseItemDmd(context, item, manifest, callback, manifest.getItemDmds(), params);

            // crosswalk content bitstreams too.
            for (Iterator ei = fileIdToBitstream.entrySet().iterator();
                 ei.hasNext();)
            {
                Map.Entry ee = (Map.Entry)ei.next();
                manifest.crosswalkBitstream(context, (Bitstream)ee.getValue(),
                                        (String)ee.getKey(), callback);
            }

            // Take a second pass over files to correct names of derived files
            // (e.g. thumbnails, extracted text) to what DSpace expects:
            for (Iterator mi = manifestContentFiles.iterator(); mi.hasNext(); )
            {
                Element mfile = (Element)mi.next();
                String bundleName = manifest.getBundleName(mfile);
                if (!bundleName.equals(Constants.CONTENT_BUNDLE_NAME))
                {
                    Element origFile = manifest.getOriginalFile(mfile);
                    if (origFile != null)
                    {
                        String ofileId = origFile.getAttributeValue("ID");
                        Bitstream obs = (Bitstream)fileIdToBitstream.get(ofileId);
                        String newName = makeDerivedFilename(bundleName, obs.getName());
                        if (newName != null)
                        {
                            String mfileId = mfile.getAttributeValue("ID");
                            Bitstream bs = (Bitstream)fileIdToBitstream.get(mfileId);
                            bs.setName(newName);
                            bs.update();
                        }
                    }
                }
            }

            // Sanity-check the resulting metadata on the Item:
            PackageUtils.checkMetadata(item);

            /* 4. Set primary bitstream; same Bundle
             */
            Element pbsFile = manifest.getPrimaryBitstream();
            if (pbsFile != null)
            {
                Bitstream pbs = (Bitstream)fileIdToBitstream.get(pbsFile.getAttributeValue("ID"));
                if (pbs == null)
                    log.error("Got Primary Bitstream file ID="+pbsFile.getAttributeValue("ID")+
                             ", but found no corresponding bitstream.");
                else
                {
                    Bundle bn[] = pbs.getBundles();
                    if (bn.length > 0)
                        bn[0].setPrimaryBitstreamID(pbs.getID());
                    else
                        log.error("Sanity check, got primary bitstream without any parent bundle.");
                }
            }

            // have subclass manage license since it may be extra package file.
            addLicense(context, collection, item, manifest, callback, license );

            // subclass hook for final checks and rearrangements
            finishItem(context, item);

            // commit any changes to bundles
            Bundle allBn[] = item.getBundles();
            for (int i = 0; i < allBn.length; ++i)
            {
                allBn[i].update();
            }

            wi.update();
            success = true;
            log.info(LogManager.getHeader(context, "ingest",
                "Created new Item, db ID="+String.valueOf(item.getID())+
                ", WorkspaceItem ID="+String.valueOf(wi.getID())));
            return wi;
        }
        catch (SQLException se)
        {
View Full Code Here


        {
            DSpaceObject resultDSO = HandleManager.resolveToObject(context, handle);

            if (resultDSO instanceof Item)
            {
              Item item = (Item) resultDSO;
             
              if (!item.isOwningCollection(collection))
                items.add(item);
            }
        }
       
        return items;
View Full Code Here

        String destDirName = null;
        String myIDString = null;
        int seqStart = -1;
        int myType = -1;

        Item myItem = null;
        Collection mycollection = null;

        if (line.hasOption('h'))
        {
            HelpFormatter myhelp = new HelpFormatter();
            myhelp.printHelp("ItemExport\n", options);
            System.out
                    .println("\nfull collection: ItemExport -t COLLECTION -i ID -d dest -n number");
            System.out
                    .println("singleitem:       ItemExport -t ITEM -i ID -d dest -n number");

            System.exit(0);
        }

        if (line.hasOption('t')) // type
        {
            typeString = line.getOptionValue('t');

            if (typeString.equals("ITEM"))
            {
                myType = Constants.ITEM;
            }
            else if (typeString.equals("COLLECTION"))
            {
                myType = Constants.COLLECTION;
            }
        }

        if (line.hasOption('i')) // id
        {
            myIDString = line.getOptionValue('i');
        }

        if (line.hasOption('d')) // dest
        {
            destDirName = line.getOptionValue('d');
        }

        if (line.hasOption('n')) // number
        {
            seqStart = Integer.parseInt(line.getOptionValue('n'));
        }

        boolean migrate = false;
        if (line.hasOption('m')) // number
        {
            migrate = true;
        }

        boolean zip = false;
        String zipFileName = "";
        if (line.hasOption('z'))
        {
            zip = true;
            zipFileName = line.getOptionValue('z');
        }

        // now validate the args
        if (myType == -1)
        {
            System.out
                    .println("type must be either COLLECTION or ITEM (-h for help)");
            System.exit(1);
        }

        if (destDirName == null)
        {
            System.out
                    .println("destination directory must be set (-h for help)");
            System.exit(1);
        }

        if (seqStart == -1)
        {
            System.out
                    .println("sequence start number must be set (-h for help)");
            System.exit(1);
        }

        if (myIDString == null)
        {
            System.out
                    .println("ID must be set to either a database ID or a handle (-h for help)");
            System.exit(1);
        }

        Context c = new Context();
        c.setIgnoreAuthorization(true);

        if (myType == Constants.ITEM)
        {
            // first, is myIDString a handle?
            if (myIDString.indexOf('/') != -1)
            {
                myItem = (Item) HandleManager.resolveToObject(c, myIDString);

                if ((myItem == null) || (myItem.getType() != Constants.ITEM))
                {
                    myItem = null;
                }
            }
            else
            {
                myItem = Item.find(c, Integer.parseInt(myIDString));
            }

            if (myItem == null)
            {
                System.out
                        .println("Error, item cannot be found: " + myIDString);
            }
        }
        else
        {
            if (myIDString.indexOf('/') != -1)
            {
                // has a / must be a handle
                mycollection = (Collection) HandleManager.resolveToObject(c,
                        myIDString);

                // ensure it's a collection
                if ((mycollection == null)
                        || (mycollection.getType() != Constants.COLLECTION))
                {
                    mycollection = null;
                }
            }
            else if (myIDString != null)
            {
                mycollection = Collection.find(c, Integer.parseInt(myIDString));
            }

            if (mycollection == null)
            {
                System.out.println("Error, collection cannot be found: "
                        + myIDString);
                System.exit(1);
            }
        }

        if (zip)
        {
            ItemIterator items;
            if (myItem != null)
            {
                items = new ItemIterator(c, new ArrayList(myItem.getID()));
            }
            else
            {
                System.out.println("Exporting from collection: " + myIDString);
                items = mycollection.getItems();
View Full Code Here

                    ItemIterator iitems = collection.getItems();
                    try
                    {
                        while (iitems.hasNext())
                        {
                            Item item = iitems.next();
                            // get all the bundles in the item
                            Bundle[] bundles = item.getBundles();
                            for (Bundle bundle : bundles)
                            {
                                // get all the bitstreams in each bundle
                                Bitstream[] bitstreams = bundle.getBitstreams();
                                for (Bitstream bit : bitstreams)
                                {
                                    // add up the size
                                    size += bit.getSize();
                                }
                            }
                            items.add(item.getID());
                        }
                    }
                    finally
                    {
                        if (iitems != null)
                            iitems.close();
                    }
                }
            }
            else if (dso.getType() == Constants.COLLECTION)
            {
                Collection collection = (Collection) dso;
                // get all the items in the collection
                ItemIterator iitems = collection.getItems();
                try
                {
                    while (iitems.hasNext())
                    {
                        Item item = iitems.next();
                        // get all thebundles in the item
                        Bundle[] bundles = item.getBundles();
                        for (Bundle bundle : bundles)
                        {
                            // get all the bitstreams in the bundle
                            Bitstream[] bitstreams = bundle.getBitstreams();
                            for (Bitstream bit : bitstreams)
                            {
                                // add up the size
                                size += bit.getSize();
                            }
                        }
                        items.add(item.getID());
                    }
                }
                finally
                {
                    if (iitems != null)
                        iitems.close();
                }
            }
            else if (dso.getType() == Constants.ITEM)
            {
                Item item = (Item) dso;
                // get all the bundles in the item
                Bundle[] bundles = item.getBundles();
                for (Bundle bundle : bundles)
                {
                    // get all the bitstreams in the bundle
                    Bitstream[] bitstreams = bundle.getBitstreams();
                    for (Bitstream bit : bitstreams)
                    {
                        // add up the size
                        size += bit.getSize();
                    }
                }
                items.add(item.getID());
            }
            else
            {
                // nothing to do just ignore this type of DSPaceObject
            }
View Full Code Here

        throws CrosswalkException,
               IOException, SQLException, AuthorizeException
    {
        if (dso.getType() != Constants.ITEM)
            throw new CrosswalkObjectNotSupported("XsltSubmissionionCrosswalk can only crosswalk to an Item.");
        Item item = (Item)dso;

        XSLTransformer xform = getTransformer(DIRECTION);
        if (xform == null)
            throw new CrosswalkInternalException("Failed to initialize transformer, probably error loading stylesheet.");
        try
View Full Code Here

    public void ingest(Context context, DSpaceObject dso, Element root)
        throws CrosswalkException, IOException, SQLException, AuthorizeException
    {
        if (dso.getType() != Constants.ITEM)
            throw new CrosswalkObjectNotSupported("XsltSubmissionionCrosswalk can only crosswalk to an Item.");
        Item item = (Item)dso;

        XSLTransformer xform = getTransformer(DIRECTION);
        if (xform == null)
            throw new CrosswalkInternalException("Failed to initialize transformer, probably error loading stylesheet.");
        try
View Full Code Here

            // iterate through the items in this collection, and count how many
            // are native, and how many are imports, and which collections they
            // came from
            while (iterator.hasNext())
            {
                Item item = iterator.next();

                if (item.isOwningCollection(collection))
                    count_native++;
                else
                    count_import++;
            }
        }
View Full Code Here

        {
            Request request = ObjectModelHelper.getRequest(objectModel);
            DSpaceObject dso = HandleUtil.obtainHandle(objectModel);
            if (dso.getType() == Constants.ITEM)
            {
                Item item = (Item) dso;
                long modSince = request.getDateHeader("If-Modified-Since");
                if (modSince != -1 && item.getLastModified().getTime() < modSince)
                {
                    return true;
                }
            }
            return false;
View Full Code Here

 
    public void addBody(Body body) throws SAXException, WingException,
            UIException, SQLException, IOException, AuthorizeException
    {
      // Get any metadata that may be removed by unselecting one of these options.
      Item item = submission.getItem();
    Collection collection = submission.getCollection();
    String actionURL = contextPath + "/handle/"+collection.getHandle() + "/workflow";
    int state = ((WorkflowItem) submission).getState();
     
      Request request = ObjectModelHelper.getRequest(objectModel);
View Full Code Here

                            + dso.getID() + ", type "
                            + Constants.typeText[dso.getType()] + ", handle "
                            + (h == null ? "null" : h));
        }

        Item item = (Item) dso;
        String handle = item.getHandle();
        List<Element> metas = new ArrayList<Element>();
        DCValue[] values = item.getMetadata(Item.ANY, Item.ANY, Item.ANY, Item.ANY);

        // Add in schema URLs e.g. <link rel="schema.DC" href="...." />
        Iterator<String> schemaIterator = schemaURLs.keySet().iterator();
        while (schemaIterator.hasNext())
        {
View Full Code Here

TOP

Related Classes of org.dspace.content.Item$MetadataCache

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.