2 Replies Latest reply: Jan 15, 2008 7:05 AM by 617460 RSS

    Could not update file with a category

    617460
      Hi Guys,
      I have a folder called Test to which i wanna apply a category called testCat. When i use the following code, i get ORACLE.FDK.MetadataAttributeRequired error if the category is made mandatory in the Folder. If the category is not made mandatory, i get an error ORACLE.FDK.UnexpectedError:ORACLE.FDK.ServerError

      Either ways i am not able to proceed. Could somebody throw light on what exactly is going wrong in the following code?

      ///-------------------------------------------------------------------------------------------------

      public static Item getItemWithCategoryInfo(FdkSession session,Item documentItem) throws FdkException, IOException, ModuleException,ServiceException{
      /* get the required item with category information*/
      Item workingItem = documentItem;
      /* Build the request for all category information on the document. */
      AttributeRequest[] categoryClassDisplayNameAttributes = new AttributeRequest[] {
      new AttributeRequest(Attributes.DISPLAY_NAME, null)
      };
      AttributeRequest[] categoryClassObjectAttributes = new AttributeRequest[] {
      new AttributeRequest(Attributes.CATEGORY_CLASS_OBJECT, categoryClassDisplayNameAttributes)
      };
      AttributeRequest[] categoryClassAttributes = new AttributeRequest[] {
      new AttributeRequest(Attributes.CLASS_NAME, null),
      new AttributeRequest(Attributes.DISPLAY_NAME, null),
      new AttributeRequest(Attributes.METADATA_ATTRIBUTES, new AttributeRequest[] {
      new AttributeRequest(Attributes.ATTRIBUTE_NAME, null),
      new AttributeRequest(Attributes.DISPLAY_NAME, null),
      new AttributeRequest(Attributes.ATTRIBUTE_TYPE, null),
      new AttributeRequest(Attributes.ATTRIBUTE_DEFAULT, null),
      new AttributeRequest(Attributes.ATTRIBUTE_ENUMERATION, null),
      new AttributeRequest(Attributes.ATTRIBUTE_REQUIRED, null),
      new AttributeRequest(Attributes.ATTRIBUTE_SETTABLE, null),
      new AttributeRequest(Attributes.ATTRIBUTE_UPDATEABLE, null),
      new AttributeRequest(Attributes.ATTRIBUTE_HIDDEN, null),
      new AttributeRequest(Attributes.ATTRIBUTE_PROMPTED, null),
      new AttributeRequest(Attributes.ATTRIBUTE_OVERRIDEABLE, null),
      })
      };
      AttributeRequest[] overrideAttributes = new AttributeRequest[] {
      new AttributeRequest(Attributes.ATTRIBUTE_OVERRIDE_ATTRIBUTE, null),
      new AttributeRequest(Attributes.ATTRIBUTE_OVERRIDE_CATEGORY_CLASS, null),
      new AttributeRequest(Attributes.ATTRIBUTE_OVERRIDE_DEFAULT, null),
      new AttributeRequest(Attributes.ATTRIBUTE_OVERRIDE_PROMPT, null),
      new AttributeRequest(Attributes.ATTRIBUTE_OVERRIDE_REQUIRED, null),
      new AttributeRequest(Attributes.ATTRIBUTE_OVERRIDE_SETTABLE, null),
      };
      AttributeRequest[] requestedAttributes = new AttributeRequest[] {
      new AttributeRequest(Attributes.CATEGORIES, categoryClassObjectAttributes),
      new AttributeRequest(Attributes.CATEGORY_CONFIGURATION, new AttributeRequest[] {
      new AttributeRequest(Attributes.CONFIGURATION_ENABLED, null),
      new AttributeRequest(Attributes.CONFIGURATION_FINAL, null),
      new AttributeRequest(Attributes.REQUIRED_CATEGORIES, categoryClassAttributes),
      new AttributeRequest(Attributes.ALLOW_ALL_CATEGORIES, null),
      new AttributeRequest(Attributes.ALLOWED_CATEGORIES, categoryClassAttributes),
      new AttributeRequest(Attributes.ATTRIBUTE_OVERRIDES, overrideAttributes)
      })
      };
      try {
      //workingItem = getCommonManager().getItem(workingItem.getId(), requestedAttributes);
      workingItem=Managers.getCommonManager(session).getItem(workingItem.getId(), requestedAttributes);
      } catch(RemoteException e) {
      e.printStackTrace();
      }
      return workingItem;
      }


      public static Item updateDoc(FdkSession session,Item doc)throws FdkException, IOException, ModuleException,ServiceException
      {

      FileManager fm = Managers.getFileManager(session);
      CategoryManager cm=Managers.getCategoryManager(session);
      AttributeRequest[] requestedAttributes = null;
      NamedValue[] sortOptions = null;
      Map returnedAttrs = null, map = null;
      Item myDoc=null;


      requestedAttributes = new AttributeRequest[]{
      // The Categories associated with this Document, if any
      new AttributeRequest(Attributes.CATEGORIES,
      // sub attributerequest
      new AttributeRequest[] {
      // the actual attributes name/values for the category instance – returns a
      // namedvalue array
      new AttributeRequest(Attributes.CUSTOM_ALL,null),
      // the actual category class for the category instance – returns an item
      new AttributeRequest(Attributes.CATEGORY_CLASS_OBJECT,null) })};


      NamedValue[] categoryInstanceAttributes = new NamedValue[]
      {  // passing the internal attribute name for the single attribute 
      new NamedValue("CUSTOM_325586", "Kuwait")};

      NamedValue[] categoryDef = new NamedValue[]
      {  // the id of the category *object* class from CONTENT schema
      new NamedValue(Options.CATEGORY_CLASS_ID,new Long(325584)),
      // the updated values of the category instance
      new NamedValue(Options.CATEGORY_DEFINITION_ATTRIBUTES, categoryInstanceAttributes)};

      NamedValue[] documentDef = new NamedValue[]
      {new NamedValue(Options.CATEGORY_DEFINITION, categoryDef)};

      myDoc = fm.updateDocument(doc.getId(),documentDef,null);

      return myDoc;
      }


      public static Item versionDocument(FdkSession session,Item folder,Item doc)throws FdkException, IOException, ModuleException,ServiceException
      {


      AttributeRequest[] requestedAttributes = null;
      NamedValue[] sortOptions = null;
      Map returnedAttrs = null, map = null;

      LockManager lm=Managers.getLockManager(session);
      lm.acquireManualLocks(new long[] { doc.getId() }, null);

      // lock attribute request
      AttributeRequest[] lock_attr = new AttributeRequest[]{
      WsUtility.newAttributeRequest(Attributes.LOCKS,
      WsUtility.newAttributeRequestArray(new String[]{
      Attributes.LOCK_TYPE, Attributes.LOCK_EXCEPTED_USER}))};

      CommonManager cm = Managers.getCommonManager(session);
      doc = cm.getItem(doc.getId(), lock_attr);

      lm.releaseManualLocks(new long[] { doc.getId() });

      applyVersioningConfig(folder,session);

      VersionManager vm = Managers.getVersionManager(session);

      Item[] workingCopies = vm.checkout(
      new long[] { doc.getId()},
      new String[] { "doc checkout comment"});


      //applying the category to the checked out file

           // step 1: update the document with category values
      updateDoc(session,workingCopies[0]);

      //step 2: finally checkin the document
      checkinDocument(workingCopies[0],session);

      return workingCopies[0];
      }


      public static void applyVersioningConfig(Item folder, FdkSession session) throws FdkException, IOException, ModuleException,ServiceException

      {


      // create manual version definition
      NamedValue[] manualVerDef = WsUtility.newNamedValueArray(new Object[][] {
      { Attributes.VERSIONING_CONFIGURATION_AUTO_LABEL, Boolean.FALSE },
      { Attributes.VERSIONING_CONFIGURATION_AUTO_VERSION, Boolean.FALSE },
      { Attributes.CONFIGURATION_FINAL, Boolean.FALSE },
      { Attributes.CONFIGURATION_ENABLED, Boolean.TRUE } });

      // before checking out Document
      // set version configuration on the parent folder
      VersionManager vm = Managers.getVersionManager(session);
      vm.setVersioningConfiguration(folder.getId(), manualVerDef);

      System.out.println("getting out of applyVersioningConfig");
      }


      public static void checkinDocument(Item workingCopy, FdkSession session) throws FdkException, IOException, ModuleException,ServiceException

      {

      // get the Manager instances
      VersionManager vm = Managers.getVersionManager(session);

      // create checkin document definition
      NamedValue[] checkInDef = WsUtility.newNamedValueArray(new Object[][] {
      { Attributes.VERSION_COMMENT, "a new version" },
      { Attributes.VERSION_LABEL, "2.0" } });

      NamedValueSet[] checkInDefs = new NamedValueSet[] {
      WsUtility.newNamedValueSet(checkInDef) };

      // check in document will be versioned
      vm.checkin(new long[] { workingCopy.getId() }, null, checkInDefs, null);

      // version attribute request
      AttributeRequest[] version_attr =
      WsUtility.newAttributeRequestArray(new String[] {
      Attributes.VERSION_LABEL,
      Attributes.VERSION_COMMENT });

      Item[] docs = vm.listVersions(workingCopy.getId(), null, version_attr);

      }

      public static Item uploadDocument(FdkSession session,String folderPath, String docName, byte[] content) throws FdkException, IOException, ModuleException,ServiceException

      {


      // get the Manager instance
      FileManager fm = Managers.getFileManager(session);

      // create an AttributeRequest[] for the URL attribute
      AttributeRequest[] urlAR =
      WsUtility.newAttributeRequestArray(Attributes.URL);


      // create a DOCUMENT_DEFINITION Item

      Item docDef = fm.createDocumentDefinition(
      WsUtility.newNamedValueArray(new Object[][] {
      { Attributes.NAME, docName } }), urlAR);

      // get the URL attribute from the docDef Item
      NamedValue[] attrs = docDef.getRequestedAttributes();
      Map attrMap = ClientUtils.namedValuesToMap(attrs);
      String docUrl = (String) attrMap.get(Attributes.URL);
      URL url = new URL(docUrl);

      // upload content into docDef using HTTP
      HTTPConnection httpCon = createHttpConnection(session,url);

      HTTPResponse rsp = httpCon.Put(url.getFile(), content);

      // check the response
      if (rsp.getStatusCode() >= 300)
      {
      System.err.println("Error: " + rsp.getReasonLine());
      }

      Item folder = fm.resolvePath(folderPath, null);

      Item newDoc = fm.createDocument(
      WsUtility.newNamedValueArray(new Object[][] {
      { Options.DESTFOLDER, new Long(folder.getId()) },
      { Options.USE_SAVED_DEFINITION, new Long(docDef.getId()) } }), null,
      urlAR);

      //call the versionDocument() to version the document before updating the file

      versionDocument(session,folder,newDoc);

      return newDoc;
      }


      public static Item createFolder(FdkSession session,String folderName, String folderDesc,Item parent) throws FdkException, RemoteException,ServiceException, IOException
      {


      FileManager fm = Managers.getFileManager(session);

      // create folder definition
      NamedValue[] folderDef =
      WsUtility.newNamedValueArray(new Object[][] {
      { Attributes.NAME, folderName },
      { Attributes.DESCRIPTION, folderDesc } });

      Item folder = fm.createFolder(parent.getId(), folderDef, null);

      return folder;
      }

      public static HTTPConnection createHttpConnection(FdkSession session,URL url)
      throws ProtocolNotSuppException
      {
      ….

      return httpCon;
      }


      }

      ///--------------------------------------------------------------------------------------------------------