/*
 * Copyright 2001-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.juddi.function;

import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.juddi.datastore.DataStore;
import org.apache.juddi.datastore.DataStoreFactory;
import org.apache.juddi.datatype.RegistryObject;
import org.apache.juddi.datatype.publisher.Publisher;
import org.apache.juddi.datatype.request.AuthInfo;
import org.apache.juddi.datatype.request.SetPublisherAssertions;
import org.apache.juddi.datatype.response.PublisherAssertions;
import org.apache.juddi.error.RegistryException;
import org.apache.juddi.registry.RegistryEngine;
import org.apache.juddi.util.Config;

/**
 * @author Steve Viens (sviens@apache.org)
 */
public class SetPublisherAssertionsFunction extends AbstractFunction
{
  // private reference to jUDDI Logger
  private static Log log = LogFactory.getLog(SetPublisherAssertionsFunction.class);

  /**
   *
   */
  public SetPublisherAssertionsFunction(RegistryEngine registry)
  {
    super(registry);
  }

  /**
   *
   */
  public RegistryObject execute(RegistryObject regObject)
    throws RegistryException
  {
    SetPublisherAssertions request = (SetPublisherAssertions)regObject;
    String generic = request.getGeneric();
    AuthInfo authInfo = request.getAuthInfo();
    Vector assertionVector = request.getPublisherAssertionVector();

    // aquire a jUDDI datastore instance
    DataStore dataStore = DataStoreFactory.getDataStore();

    try
    {
      dataStore.beginTrans();

      // validate authentication parameters
      Publisher publisher = getPublisher(authInfo,dataStore);
      String publisherID = publisher.getPublisherID();

      // validate request parameters & execute
      // nothing that requires validation has been identified

      // set the PublisherAssertions
      Vector savedAssertionsVector = dataStore.setAssertions(publisherID,assertionVector);

      dataStore.commit();

      PublisherAssertions assertions = new PublisherAssertions();
      assertions.setGeneric(generic);
      assertions.setOperator(Config.getOperator());
      assertions.setPublisherAssertionVector(savedAssertionsVector);
      return assertions;
    }
    catch(RegistryException regex)
    {
      try { dataStore.rollback(); } catch(Exception e) { }
      log.error(regex);
      throw (RegistryException)regex;
    }
    catch(Exception ex)
    {
      try { dataStore.rollback(); } catch(Exception e) { }
      log.error(ex);
      throw new RegistryException(ex);
    }
    finally
    {
      if (dataStore != null)
        dataStore.release();
    }
  }


  /***************************************************************************/
  /***************************** TEST DRIVER *********************************/
  /***************************************************************************/


  public static void main(String[] args)
  {
    // initialize the registry
    RegistryEngine reg = new RegistryEngine();
    reg.init();

    try
    {
    }
    catch (Exception ex)
    {
      // write execption to the console
      ex.printStackTrace();
    }
    finally
    {
      // destroy the registry
      reg.dispose();
    }
  }
}


@@ -1135,32 +1135,144 @@
 sherAssertions;%0A
+import org.apache.juddi.datatype.assertion.PublisherAssertion;%0Aimport org.apache.juddi.datatype.KeyedReference;%0A
 import org.apach
@@ -1353,16 +1353,73 @@
 Engine;%0A
+import org.apache.juddi.error.InvalidKeyPassedException;%0A
 import o
@@ -2500,16 +2500,1018 @@
 execute%0A
+      // Per UDDI v2.0 specification, the tModelKey in a passed publisherAssertion CANNOT be null or blank.  An invalidKeyPassed error must be thrown.%0A      if (assertionVector != null) %7B%0A    %09  for (int i = 0; i %3C assertionVector.size(); i++) %7B%0A    %09%09  PublisherAssertion publisherAssertion = (PublisherAssertion)assertionVector.elementAt(i);%0A    %09%09  if (publisherAssertion != null) %7B%0A    %09%09%09  KeyedReference kr = publisherAssertion.getKeyedReference();%0A    %09%09%09  if (kr != null) %7B%0A    %09%09%09%09  if (kr.getTModelKey() == null %7C%7C kr.getTModelKey().length() == 0) %7B%0A    %09%09%09%09%09  //throw new InvalidKeyPassedException(%22tModelKey must be provided in KeyedReference for publisherAssertion: fromKey=%22 + %0A    %09%09%09%09%09//%09%09                               publisherAssertion.getFromKey() + %22; toKey=%22 + publisherAssertion.getToKey());%0A  %09%09%09%09%09  throw new InvalidKeyPassedException(%22tModelKey must be provided in KeyedReference for publisherAssertion: fromKey=%22);%0A%0A    %09%09%09%09  %7D%0A    %09%09%09  %7D%0A    %09%09  %7D%0A    %09%09  %0A    %09  %7D%0A      %7D%0A
       //
@@ -3969,32 +3969,208 @@
 sertions;%0A    %7D%0A
+    catch(InvalidKeyPassedException ivkex)%0A    %7B%0A      try %7B dataStore.rollback(); %7D catch(Exception e) %7B %7D%0A      log.error(ivkex);%0A      throw (RegistryException)ivkex;%0A    %7D%0A
     catch(Regist

Fix for juddi-78. Per uddi v2 spec, tmodel key of keyedreference cannot be null. If null, an invalidKeyPassed fault is thrown.