/*******************************************************************
 * Main routine for the PCM_OP_CREATE_METAFIELD operation.
 *******************************************************************/
void
op_create_metafield(
    cm_nap_connection_t     *connp,
    int32                    opcode,
    int32                    flags,
    pin_flist_t             *in_flistp,
    pin_flist_t            **ret_flistpp,
    pin_errbuf_t            *ebufp)
{
    pcm_context_t           *ctxp = connp->dm_ctx;
    void				*vp =NULL;

    PIN_ERR_CLEAR_ERR(ebufp);

    /***********************************************************
     * Insanity check.
     ***********************************************************/
    if (opcode != PCM_OP_CREATE_METAFIELD) {
        pin_set_err(ebufp, PIN_ERRLOC_FM, PIN_ERRCLASS_SYSTEM_DETERMINATE, PIN_ERR_BAD_OPCODE, 0, 0, opcode);
        PIN_ERR_LOG_EBUF(PIN_ERR_LEVEL_ERROR, "op_create_metafield opcode error", ebufp);
        return;
    }

    /***********************************************************
     * Debug: What we got.
     ***********************************************************/
    PIN_ERR_LOG_FLIST(PIN_ERR_LEVEL_DEBUG, "op_create_metafield input flist", in_flistp);

    /***********************************************************
     * Do the actual op in a sub-function.
     ***********************************************************/
    fm_cust_pol_create_metafield_obj(ctxp, flags, in_flistp, ret_flistpp, ebufp);

    /***********************************************************
     * Error?
     ***********************************************************/
    if (PIN_ERR_IS_ERR(ebufp))
    {
        PIN_ERR_LOG_EBUF(PIN_ERR_LEVEL_ERROR, "op_create_metafield error", ebufp);
        PIN_ERR_LOG_FLIST(PIN_ERR_LEVEL_ERROR, "op_create_metafield return flist", *ret_flistpp);
    }
    else
    {
        /***************************************************
         * Debug: What we're sending back.
         ***************************************************/
        PIN_ERR_LOG_FLIST(PIN_ERR_LEVEL_DEBUG, "op_create_metafield return flist", *ret_flistpp);
        vp = PIN_FLIST_FLD_GET(*ret_flistpp,PIN_FLD_POID,1,ebufp);

        if(PIN_POID_IS_NULL(vp)==0)
            PIN_ERR_LOG_POID(PIN_ERR_LEVEL_DEBUG, "metafield poid created is", vp);
    }
    return;
}
/*
 * Search for a brand name within a brand.
 */
static void
fm_cust_pol_is_brandname_unique_within_brand(
	pcm_context_t		*ctxp,
	poid_t			*poidp,
	char			*namep,
	pin_flist_t		**r_flistpp,
	pin_errbuf_t		*ebufp)
{
	pin_flist_t		*flistp = (pin_flist_t *)NULL;
	pin_flist_t		*rlistp = (pin_flist_t *)NULL;
	pin_flist_t		*sublistp;
	poid_t			*search_poidp;
	int32			int_val;
	int32			cred;
	pin_account_type_t	brand_type = PIN_ACCOUNT_TYPE_BRAND;
	pin_cookie_t		cookie = (pin_cookie_t)NULL;
	pin_flist_t             *b_flistp = NULL;
	pin_flist_t             *t_flistp = NULL;
	poid_t                  *b_pdp = NULL;
	poid_t                  *g_pdp = NULL;
	poid_t                  *rootp = NULL;
	int32                   elemid = 0;
	int			under_host = PIN_BOOLEAN_FALSE;
	void			*vp = NULL;

	/*
	 * If there are pending errors, then short circuit immediately
	 */
        if (PIN_ERR_IS_ERR(ebufp))
                return;
        PIN_ERR_CLEAR_ERR(ebufp);

	fm_utils_lineage_get_brand_hierarchy(ctxp, poidp, &b_flistp, ebufp);
	if (PIN_ERR_IS_ERR(ebufp)) {
		PIN_ERR_LOG_EBUF(PIN_ERR_LEVEL_ERROR,
			"fm_cust_pol_is_brandname_unique_within_brand() : "
			"Call to get brand hierarchy failed ",
			ebufp);
	 	PIN_FLIST_DESTROY_EX (&b_flistp, NULL);
		fm_cust_pol_read_err_return_list(poidp, namep, r_flistpp,
			ebufp);
                goto cleanup;
        }

        PIN_ERR_LOG_FLIST(PIN_ERR_LEVEL_DEBUG,
		"Got brand hierarchy.  Return flist :", b_flistp);

	t_flistp = PIN_FLIST_ELEM_GET_NEXT(b_flistp,
		PIN_FLD_RESULTS, &elemid, 1, &cookie, ebufp);
	if (PIN_ERR_IS_ERR(ebufp) || t_flistp == (pin_flist_t *)NULL) {
		PIN_ERR_LOG_EBUF(PIN_ERR_LEVEL_ERROR,
			"fm_cust_pol_is_brandname_unique_within_brand() : "
			"Call to get first brand failed ",
			ebufp);
		PIN_FLIST_DESTROY_EX (&b_flistp, NULL);
		fm_cust_pol_read_err_return_list(poidp, namep, r_flistpp,
			ebufp);
                goto cleanup;
	}
	
	/********************************************
	 * Get the first brand object from hierarchy
	 ********************************************/
	b_pdp = PIN_FLIST_FLD_GET(t_flistp, PIN_FLD_BRAND_OBJ, 0, ebufp);

	/***************************************************************
	 * During the brand creation, the first brand object from the
	 * the hierarchy is the parent brand. But, for an existing 
	 * brand, the first object is self, the next one in hierarchy 
	 * is the parent brand.
	 ***************************************************************/
	if (!PIN_POID_COMPARE(b_pdp, poidp, 0, ebufp)) {
		PIN_ERR_LOG_MSG(PIN_ERR_LEVEL_DEBUG, "first brand is self");
		t_flistp = PIN_FLIST_ELEM_GET_NEXT(b_flistp,
			PIN_FLD_RESULTS, &elemid, 1, &cookie, ebufp);
		if (PIN_ERR_IS_ERR(ebufp) || t_flistp == (pin_flist_t *)NULL) {
			PIN_ERR_LOG_EBUF(PIN_ERR_LEVEL_ERROR,
			"fm_cust_pol_is_brandname_unique_within_brand() : "
			"Call to get second brand failed ",
			ebufp);
			PIN_FLIST_DESTROY_EX (&b_flistp, NULL);
			fm_cust_pol_read_err_return_list(poidp, namep, 	
				r_flistpp, ebufp);
                	goto cleanup;
		}
	 	/* Get the parent brand object */
		b_pdp = PIN_FLIST_FLD_GET(t_flistp, PIN_FLD_BRAND_OBJ, 
			0, ebufp);
	}
	else {
		PIN_ERR_LOG_MSG(PIN_ERR_LEVEL_DEBUG, "first brand is parent");
	}

	PIN_ERR_LOG_POID(PIN_ERR_LEVEL_DEBUG, "parent brand = ", b_pdp);

	/***************************************************************
	 * Check to see if we are creating brand under brandhost
	 ***************************************************************/
	rootp = PIN_POID_CREATE(PIN_POID_GET_DB(poidp), "/account", 1, ebufp);
	if (!PIN_POID_COMPARE(b_pdp, rootp, 0, ebufp)) {
		under_host = PIN_BOOLEAN_TRUE;  /* creating brand under 
						   brandhost */
	}
	else {	/* creating sub-brand under a brand */
		/* get the group_obj_id0 for the parent brand */
		cred = CM_FM_BEGIN_OVERRIDE_SCOPE(CM_CRED_SCOPE_OVERRIDE_ROOT); 
		g_pdp = fm_utils_lineage_get_billing_group_by_parent(ctxp, 
			b_pdp, ebufp);
		CM_FM_END_OVERRIDE_SCOPE(cred); 
		if (PIN_ERR_IS_ERR(ebufp) || g_pdp == (poid_t*)NULL) {
			PIN_ERR_LOG_EBUF(PIN_ERR_LEVEL_ERROR,
			"fm_cust_pol_is_brandname_unique_within_brand() : "
			"Call to get parent group failed ",
			ebufp);
			PIN_POID_DESTROY(rootp, NULL);
			PIN_FLIST_DESTROY_EX (&b_flistp, NULL);
			fm_cust_pol_read_err_return_list(poidp, namep, 
				r_flistpp, ebufp);
                	goto cleanup;
		}
	}
	PIN_POID_DESTROY(rootp, NULL);
	PIN_FLIST_DESTROY_EX (&b_flistp, NULL);
	
	/*
	 * Create search flist to try to figure out if someother brand already
	 * has this name within the parent brand.
	 */
	flistp = PIN_FLIST_CREATE(ebufp);
	search_poidp = PIN_POID_CREATE(PIN_POID_GET_DB(poidp), "/search", -1,
		ebufp);
	PIN_FLIST_FLD_PUT(flistp, PIN_FLD_POID, (void *)search_poidp, ebufp);
	int_val = SRCH_DISTINCT;
	PIN_FLIST_FLD_SET(flistp, PIN_FLD_FLAGS, (void *)&int_val, ebufp);

	/***************************************************************
	 * We need different search templates for checking duplicate
	 * brandnames under the brandhost or another brand.
	 * The brandhost doesnot have an entry in the /group/billing
	 * storable class. The only way we can get the names of the 
	 * existing brands under the brandhost is to check the lineage
	 * tag. Arguments 1 to 3 are the same for both search templates,
	 * but arguments 4 & 5 are different. 
	 ***************************************************************/
	if (under_host == PIN_BOOLEAN_FALSE) {  
		/* creating sub-brands under a brand */
		PIN_FLIST_FLD_SET(flistp, PIN_FLD_TEMPLATE, 
			(void *)TEMPLATE_UNIQUE_UNDER_BRAND, ebufp);
	}
	else {                   /* creating brand under brandhost */
		PIN_FLIST_FLD_SET(flistp, PIN_FLD_TEMPLATE, 
			(void *)TEMPLATE_UNIQUE_UNDER_BRANDHOST, ebufp);
	}
	PIN_FLIST_ELEM_SET(flistp, NULL, PIN_FLD_RESULTS, 0, ebufp);

	/*
	 * Arg 1 is the type of the account; only interested in brand accounts
	 */
	sublistp = PIN_FLIST_ELEM_ADD(flistp, PIN_FLD_ARGS, 1, ebufp);
	PIN_FLIST_FLD_SET(sublistp, PIN_FLD_ACCOUNT_TYPE, (void *)&brand_type,
		ebufp);

	/*
	 * Arg 2 is the name of the brand.
	 */
	sublistp = PIN_FLIST_ELEM_ADD(flistp, PIN_FLD_ARGS, 2, ebufp);
	PIN_FLIST_FLD_SET(sublistp, PIN_FLD_NAME, (void *)namep, ebufp);

	/*
	 * Arg 3 -- POID of account
	 */
	sublistp = PIN_FLIST_ELEM_ADD(flistp, PIN_FLD_ARGS, 3, ebufp);
	PIN_FLIST_FLD_PUT(sublistp, PIN_FLD_POID, NULL, ebufp);

	if (under_host == PIN_BOOLEAN_FALSE) {  
		/* creating sub-brands under a brand */
		/*
	 	 * Arg 4 -- OBJECT_ID of members from /group/billing 
	 	 */
		sublistp = PIN_FLIST_ELEM_ADD(flistp, PIN_FLD_ARGS, 4, ebufp);
		sublistp = PIN_FLIST_ELEM_ADD(sublistp, PIN_FLD_MEMBERS, 
			PIN_ELEMID_ANY, ebufp);
        	PIN_FLIST_FLD_PUT(sublistp, PIN_FLD_OBJECT, (void *)NULL, 
			ebufp);

		/*
	 	 * Arg 5 -- POID of the group
	 	 */
		sublistp = PIN_FLIST_ELEM_ADD(flistp, PIN_FLD_ARGS, 5, ebufp);
		PIN_FLIST_FLD_SET(sublistp, PIN_FLD_POID, (void *)g_pdp, ebufp);
	}
	else {                   /* creating brand under brandhost */
		/*
	 	 * Arg 4 -- Parent of the group
	 	 */
		sublistp = PIN_FLIST_ELEM_ADD(flistp, PIN_FLD_ARGS, 4, ebufp);
		PIN_FLIST_FLD_PUT(sublistp, PIN_FLD_PARENT, NULL, ebufp);

		/*
	 	 * Arg 5 -- LINEAGE of account
	 	 */
		/* accounts under brandhost have only one ":" in their
		   lineage tag. We want to exclude others. */

		vp = (void *)"%:%:%";	
		sublistp = PIN_FLIST_ELEM_ADD(flistp, PIN_FLD_ARGS, 5, ebufp);
		PIN_FLIST_FLD_SET(sublistp, PIN_FLD_LINEAGE, vp, ebufp);
	}

        /*
         * Arg 6 -- POID of current account
         */
        sublistp = PIN_FLIST_ELEM_ADD(flistp, PIN_FLD_ARGS, 6, ebufp);
        PIN_FLIST_FLD_SET(sublistp, PIN_FLD_POID, poidp, ebufp);

	/*
	 * For completeness, dump the input flist if debugging is enabled.
	 */
        PIN_ERR_LOG_FLIST(PIN_ERR_LEVEL_DEBUG,
                          "fm_cust_pol_is_brandname_unique_within_brand search input flist:",
                          flistp);

	/*
	 * Ready to search! Temporarily suspend search to ensure that
	 * all brands are traversed.
	 */
	cred = CM_FM_BEGIN_OVERRIDE_SCOPE(CM_CRED_SCOPE_OVERRIDE_ROOT);
	PCM_OP(ctxp, PCM_OP_GLOBAL_SEARCH, PCM_OPFLG_COUNT_ONLY, 
		flistp, &rlistp, ebufp);
	CM_FM_END_OVERRIDE_SCOPE(cred);

	/*
	 * For completeness, dump the results flist if debugging is enabled
	 * and we actually have a results flist.
	 */
	if(rlistp != (pin_flist_t *)NULL) {
		PIN_ERR_LOG_FLIST(PIN_ERR_LEVEL_DEBUG,
			"fm_cust_pol_is_brandname_unique_within_brand search output flist:",
			rlistp);
	}

	/* 
	 * See if there is a brand out there right now that already has
	 * this name. If so, flag it as an error.
	 */
	cookie = (pin_cookie_t)NULL;
	int_val = 0;
	PIN_FLIST_ELEM_GET_NEXT(rlistp, PIN_FLD_RESULTS, &int_val, 1, 
		&cookie, ebufp);
	if(int_val > 0) {
		*r_flistpp = PIN_FLIST_CREATE(ebufp);
		PIN_FLIST_FLD_SET(*r_flistpp, PIN_FLD_POID, (void *)poidp, 
			ebufp);
		fm_cust_pol_valid_add_fail(*r_flistpp, 
			PIN_FLD_NAME,
			0,
			PIN_CUST_VAL_ERR_DUPE,
			PIN_CUST_DUPE_VALUE_ERR_MSG,
			(void *)namep, ebufp);

		pin_set_err(ebufp, PIN_ERRLOC_FM,
			PIN_ERRCLASS_SYSTEM_DETERMINATE,
			PIN_ERR_DUPLICATE, PIN_FLD_NAME, 
			0, 0);
	}

cleanup:
	/*
	 * Cleanup and return
	 */
	PIN_FLIST_DESTROY_EX(&flistp, NULL);
	PIN_FLIST_DESTROY_EX(&rlistp, NULL);
	PIN_POID_DESTROY(g_pdp, NULL);
}