bundle.setPrimaryBitstreamID(primaryBitstreamID);
if (primaryBitstreamID > 0) { bundle.setPrimaryBitstreamID(primaryBitstreamID); }
    private void processUpdateItem(Context context,        HttpServletRequest request,        HttpServletResponse response,        Item item)        throws ServletException, IOException, SQLException, AuthorizeException    {        String button = UIUtil.getSubmitButton(request, "submit");        /*         * "Cancel" handled above, so whatever happens, we need to update the         * item metadata.  First, we remove it all, then build it back up         * again.         */        item.clearDC(Item.ANY, Item.ANY, Item.ANY);        // We'll sort the parameters by name.  This ensures that DC fields        // of the same element/qualifier are added in the correct sequence.        // Get the parameters names        Enumeration unsortedParamNames = request.getParameterNames();        // Put them in a list        List sortedParamNames = new LinkedList();        while (unsortedParamNames.hasMoreElements())        {            sortedParamNames.add(unsortedParamNames.nextElement());        }        // Sort the list        Collections.sort(sortedParamNames);        Iterator iterator = sortedParamNames.iterator();        while (iterator.hasNext())        {            String p = (String) iterator.next();            if (p.startsWith("value"))            {                /*                 * It's a metadata value - it will be of the form                 *   value_element_1              OR                 *   value_element_qualifier_2                 * (the number being the sequence number)                 * We use a StringTokenizer to extract these values                 */                StringTokenizer st = new StringTokenizer(p, "_");                st.nextToken();   // Skip "value"                String element = st.nextToken();                String qualifier = null;                if (st.countTokens() == 2)                {                    qualifier = st.nextToken();                }                String sequenceNumber = st.nextToken();                // Get a string with "element" for unqualified or                // "element_qualifier"                String key = element;                if (qualifier != null)                {                    key = element + "_" + qualifier;                }                // Get the language                String language = request.getParameter(                    "language_" + key + "_" + sequenceNumber);                // Empty string language = null                if (language != null && language.equals(""))                {                    language = null;                }                // Get the value                String value = request.getParameter(p).trim();                // If remove button pressed for this value, we don't add it                // back to the item.  We also don't add empty values.                if (value.equals("") ||                    !button.equals("submit_remove_" + key + "_" + sequenceNumber))                {                    // Value is empty, or remove button for this wasn't pressed                    item.addDC(element, qualifier, language, value);                }            }            else if (p.startsWith("bitstream_name"))            {                // We have bitstream metadata                // First, get the bundle and bitstream ID                // Parameter name is bitstream_name_(bundleID)_(bitstreamID)                StringTokenizer st = new StringTokenizer(p, "_");                // Ignore "bitstream" and "name"                st.nextToken();                st.nextToken();                // Bundle ID and bitstream ID next                int bundleID = Integer.parseInt(st.nextToken());                int bitstreamID = Integer.parseInt(st.nextToken());                Bundle bundle = Bundle.find(context, bundleID);                Bitstream bitstream = Bitstream.find(context, bitstreamID);                // Get the string "(bundleID)_(bitstreamID)" for finding other                // parameters related to this bitstream                String key = String.valueOf(bundleID) + "_" + bitstreamID;                // Update bitstream metadata, or delete?                if (button.equals("submit_delete_bitstream_" + key))                {                    // "delete" button pressed                    bundle.removeBitstream(bitstream);                    // Delete bundle too, if empty                    if (bundle.getBitstreams().length == 0)                    {                        item.removeBundle(bundle);                    }                }                else                {                    // Update the bitstream metadata                    String name = request.getParameter(p);                    String source =                        request.getParameter("bitstream_source_" + key);                    String desc =                        request.getParameter("bitstream_description_" + key);                    int formatID = UIUtil.getIntParameter(request,                        "bitstream_format_id_" + key);                    String userFormatDesc = request.getParameter(                        "bitstream_user_format_description_" + key);		    int primaryBitstreamID = UIUtil.getIntParameter(request,			bundleID + "_primary_bitstream_id");                    // Empty strings become non-null                    if (source.equals("")) source = null;                    if (desc.equals("")) desc = null;                    if (userFormatDesc.equals("")) userFormatDesc = null;                    bitstream.setName(name);                    bitstream.setSource(source);                    bitstream.setDescription(desc);                    bitstream.setFormat(                        BitstreamFormat.find(context, formatID));		    bundle.setPrimaryBitstreamID(primaryBitstreamID);                    if (userFormatDesc != null)                    {                        bitstream.setUserFormatDescription(userFormatDesc);                    }                    bitstream.update();		    bundle.update();                }            }        }        item.update();        /*         * Now respond to button presses, other than "Remove" or "Delete"         * button presses which were dealt with in the above loop.         */        if (button.equals("submit_addfield"))        {            // Adding a metadata field            int dcTypeID = UIUtil.getIntParameter(request, "addfield_dctype");            String value = request.getParameter("addfield_value").trim();            String lang = request.getParameter("addfield_language");            // Empty language = null            if (lang.equals("")) lang = null;            DCType dcType = DCType.find(context, dcTypeID);            item.addDC(dcType.getElement(), dcType.getQualifier(), lang, value);            item.update();        }        if (button.equals("submit_addbitstream"))        {            // Show upload bitstream page            request.setAttribute("item", item);            JSPManager.showJSP(request, response,                "/tools/upload-bitstream.jsp");        }        else        {            // Show edit page again            showEditForm(context, request, response, item);        }        // Complete transaction        context.complete();    }