/** 
* @addtogroup			workflow
* @{
*/

/**
* @file					epm_register_handler.cpp
* @brief				itk user exits function implemention
* @date					2009/2/13
* @author				James Pang
* @history
* ===================================================================================
* Date					Name				Description
* 13-Feb-2009			James				created
*/

#pragma warning (disable: 4996) 
#pragma warning (disable: 4819) 

/**
* @headerfile			tcua 头文件
*/

#include <tccore/custom.h>
#include <tccore/workspaceobject.h>
#include <server_exits/user_server_exits.h>
#include <epm/epm.h>
#include <sa/user.h>
#include <tccore/item.h>
#include <tccore/grmtype.h>
#include <tccore/grm.h>
#include <epm/signoff.h>
#include <ae/ae.h>
#include <sa/tcfile.h>
#include <user_exits/user_exits.h>
#include <tc/emh.h>
#include <tccore/aom.h>
#include <tccore/aom_prop.h>


/**
* @headerfile			cpp 头文件
*/
//#include "stdafx.h"
#include <windows.h>
#include <shlobj.h>
#pragma comment( lib, "shell32.lib")
#include <stdio.h>
#include <time.h>

#include <string>
#include <vector>
#include <map>
using namespace std;

#include "register_handler_methods.h"
//#include "register_handler_head.h"
#include "error_handling.h"
#include "string_helper.h"
#include "common_itk_util.h"


int Origin_process_msexcel(tag_t dataset, char *dat_file);
int Origin_process_msword(tag_t dataset, char *dat_file);
int Origin_process_ugmaster( tag_t dataset, char *dat_file);
int Origin_process_autocad( tag_t dataset, char *dat_file);
int Origin_process_proe(tag_t dataset, char *dat_file);
//int Origin_create_temp_file(char *file_content, char *file_name);
int Origin_create_temp_file(char *file_content, char *item_id, char *file_name);
int Origin_process_acad(tag_t dataset, char *dat_file);
#define random(x) (rand()%x)
static int create_rand_number(int *randnum)
{
	srand( (unsigned)time( NULL ) );
	//double d;
	//d = random(1000)/7*10000000;
	//sprintf   (yourID,   size,   "0x80x8",   time   (0),   rand());
	*randnum=random(1000000)+(long)GetTickCount();
	
	//*randnum=int(d);
	return 0;
}
int Sample2( tag_t cur_task, char *specified_task_node, tag_t *found_task )
{
	tag_t job, root_task, root_template;	
	EPM_ask_job( cur_task, &job );
	EPM_ask_root_task( job, &root_task );
	
	

	int sub_task_num = 0;
	tag_t *sub_tasks = NULL, the_task = NULLTAG;
	EPM_ask_sub_tasks( root_task, &sub_task_num, &sub_tasks );
	for ( int i=0; i<sub_task_num; i++ )
	{
		char* task_name = NULL;
		EPM_ask_name2( sub_tasks[i], &task_name );

		if ( stricmp( task_name, specified_task_node ) == 0 )
		{
			the_task = sub_tasks[i];
			break;
		}
	}
	MEM_free( sub_tasks );

	if ( the_task == NULLTAG )
	{
		printf( "no specified task\n" );
		return 919100;//one error code
	}

	char* cur_task_name = NULL;
	EPM_ask_name2( cur_task, &cur_task_name );
	
	logical is_valid_node = FALSE;
	int num_success = 0;
	tag_t *successors = NULL;
	AOM_ask_value_tags( the_task, "successors", &num_success, &successors );
	do 
	{
		if ( num_success == 0 )
			break;

		tag_t one_successor = successors[0];//一般情况都只有一个后继
		MEM_free( successors );

		char* successor_name = NULL;
		EPM_ask_name2( one_successor, &successor_name );
		if ( strcmp( successor_name, cur_task_name ) == 0 )
		{
			is_valid_node = TRUE;
			*found_task = one_successor;
			break;
		}
		
		AOM_ask_value_tags( one_successor, "successors", &num_success, &successors );
	} while ( num_success>0 );
	
	if ( !is_valid_node )
		*found_task = NULLTAG;

	return ITK_ok;
}

int ORIGIN_EPM_check_and_signoff( EPM_action_message_t msg )
{
	tag_t specified_task = NULLTAG;
	printf( "in ORIGIN_EPM_check_and_signoff\n" );

	char *cur_task_name = NULL;
	ITKCALL( EPM_ask_name2( msg.task, &cur_task_name ) );

	if ( stricmp( cur_task_name, "perform-signoffs" ) == 0 )
	{
		ITKCALL( EPM_ask_parent_task( msg.task, &specified_task ) );
	}
	else
		specified_task = msg.task;

	char* task_type = NULL;
	ITKCALL( WSOM_ask_object_type2( specified_task, &task_type ) );
	if ( stricmp( task_type, "EPMReviewTask" ) != 0 )
	{
		return ITK_ok;
	}
	
	//取意见或者做别的事情

	return ITK_ok;
}

static int CheckTaskValid(EPM_action_message_t msg, logical *is_demote )
{
	tag_t cur_task = NULLTAG;
	char* task_type = NULL;
	ITKCALL( WSOM_ask_object_type2( msg.task, &task_type ) );
	if ( stricmp( task_type, "EPMPerformSignoffTask" ) != 0 )
	{
		printf(("current task type is not EPMPerformSignoffTask\n" ));
		return HANDLER_PLACED_INVALID;
	}

}


int ORIGIN_ask_task_node(tag_t cur_task, char *task_node_name, tag_t *task_node)
{
	tag_t job, root_task;
	char buf[BUFSIZ] = "";

	ITKCALL(EPM_ask_job(cur_task, &job));
	ITKCALL(EPM_ask_root_task(job, &root_task));

	int sub_task_num = 0;
	tag_t *sub_tasks = NULL, the_task = NULLTAG;
	ITKCALL(EPM_ask_sub_tasks(root_task, &sub_task_num, &sub_tasks));
	for (int i = 0; i < sub_task_num; i++)
	{
		char* task_name = NULL;
		ITKCALL(EPM_ask_name2(sub_tasks[i], &task_name));
		if (stricmp(task_name, task_node_name) == 0)
		{
			the_task = sub_tasks[i];
			break;
		}
	}
	MEM_free( sub_tasks );

	if (the_task == NULLTAG)
	{
		sprintf( buf, "no specified task\n" );
		printf( (buf) );
		return WORKFLOW_NODE_IS_NOT_VALID;
	}

	char* cur_task_name = NULL;
	ITKCALL(EPM_ask_name2(cur_task, &cur_task_name)); printf( "cur task = %s\n", cur_task_name );

	logical is_valid_node = FALSE;
	int num_success = 0;
	tag_t *successors = NULL;
	ITKCALL(AOM_ask_value_tags(the_task, "successors", &num_success, &successors));

	if(num_success != 0)
	{
		do 
		{
			tag_t one_successor = successors[0];
			MEM_free(successors);

			char* successor_name = NULL;
			ITKCALL(EPM_ask_name2(one_successor, &successor_name));
			if (strcmp(successor_name, cur_task_name) == 0)
			{
				is_valid_node = TRUE;
				*task_node = the_task;
				break;
			}

			ITKCALL(AOM_ask_value_tags(one_successor, "successors", &num_success, &successors));
		} while (num_success > 0);
	}

	if (!is_valid_node)
	{
		*task_node = NULLTAG;
	}

	return ITK_ok;
}

int ORIGIN_ask_sign_info(tag_t task_node, char *output_str,char *task_name)
{
	//strcpy(output_str, "");
	tag_t cur_perform_task = NULLTAG, tempTask = NULLTAG;
	char* cur_task_name = NULL, buf[BUFSIZ] = "";

	ITKCALL( EPM_ask_name2( task_node, &cur_task_name ) );
	printf("cur_task_name = %s\n",cur_task_name);

	if ( stricmp( cur_task_name, "perform-signoffs" ) == 0 )
	{
		cur_perform_task = task_node;
		ITKCALL( EPM_ask_parent_task( cur_perform_task, &tempTask ) );
		ITKCALL( EPM_ask_name2( tempTask, &cur_task_name ) );
	}
	else if( strcmp( cur_task_name, "编制" ) == 0 )
	{
		cur_perform_task = task_node;
	}
	else
	{
		ITKCALL( EPM_ask_sub_task(task_node, "perform-signoffs", &cur_perform_task) );
		if ( cur_perform_task != NULLTAG )
		{
			ITKCALL( EPM_ask_name2( task_node, &cur_task_name ) );
		}
	}
	//cur_perform_task = task_node;
	//printf("output_str=%s\n",output_str);
	if(cur_perform_task != NULLTAG )
	{
		//printf("output_str=%s\n",output_str);
		//if( strcmp( cur_task_name, "编制" ) == 0 )
		//{
		//	printf("编制!================\n");
		//	//printf("output_str=%s\n",output_str);
		//	
		//	printf("task_name=%s\n",task_name);
  //          int s;
  //          EPM_decision_t decision = EPM_nogo;
  //          char* userName;
  //          tag_t aUserTag, responsibleParty;

		//	s = EPM_ask_responsible_party(cur_perform_task, &responsibleParty);
  //          if (responsibleParty!= NULLTAG)
  //          {
  //              decision = EPM_go;
  //          } 
		//	
  //          s = POM_get_user (&userName, &aUserTag);
		//	date_t decision_date;
		//	char person_name[SA_name_size_c + 1] = "";

		//	//if( strstr(output_str,"编制")==NULL )
		//	{
		//		ITKCALL(SA_ask_user_person_name(aUserTag, person_name));
		//		strcat(output_str, "|");
		//		strcat(output_str, cur_task_name);
		//		strcat(output_str, "=");
		//		strcat(output_str, person_name);
		//		strcat(output_str, "|");
		//		strcat(output_str, cur_task_name);
		//		strcat(output_str, "日期=");
		//		char*   prop_name="last_mod_date";//或date_released
		//		
		//		ITKCALL(AOM_ask_value_date(cur_perform_task,prop_name,&decision_date));
		//		char date_buf[20] = "";
		//		sprintf( date_buf, "%4d-%02d-%02d", decision_date.year, decision_date.month+1, decision_date.day );
		//		strcat( output_str, date_buf );
		//		printf(output_str);
		//		printf("\n");
		//	}
		//	
  //          MEM_free (userName); 
  //          
		//}
		//else
		{
			int perform_count = 0;
			int *attach_type;
			tag_t *perform_attaches = NULL;
			
			//ITKCALL(EPM_ask_attachments(cur_perform_task, EPM_signoff_attachment, &perform_count, &perform_attaches));
			ITKCALL(EPM_ask_all_attachments(cur_perform_task,&perform_count,&perform_attaches,&attach_type));
			sprintf( buf, "EPM_signoff_attachment Counts = %d\n", perform_count);
			if(perform_count ==0)
			{
				//ITKCALL(EPM_ask_attachments(cur_perform_task, EPM_target_attachment, &perform_count, &perform_attaches));

			sprintf( buf, "perform_count = %d\n", perform_count);
			printf(buf);
			//printf( (buf) );
			}
			for(int i = 0; i < perform_count; i++)
			{
				//printf("attach_type = %d\n", attach_type[i]);
				tag_t memberTag = NULLTAG;
				SIGNOFF_TYPE_t memberType;
				//CR_signoff_decision_t signoff_decision;
				EPM_signoff_decision_t signoff_decision;
				char* comments = NULL;
				date_t decision_date;
				char* person_name = NULL;

				ITKCALL(EPM_ask_signoff_member(perform_attaches[i], &memberTag, &memberType));
				//ITKCALL(CR_ask_signoff_decision(perform_attaches[i], &signoff_decision, comments, &decision_date));
				ITKCALL(EPM_ask_signoff_decision(perform_attaches[i], &signoff_decision, &comments, &decision_date));
				if(memberType == SIGNOFF_GROUPMEMBER )//&& strstr(cur_task_name,"编制")==NULL)
				{
					printf("\ncur_task_name = %s\n",cur_task_name);
					tag_t user_tag = NULLTAG, group_tag = NULLTAG, role_tag = NULLTAG;

					ITKCALL(SA_ask_groupmember_user(memberTag, &user_tag));
					ITKCALL(SA_ask_user_person_name2(user_tag, &person_name));

					strcat(output_str, "|");
					strcat(output_str, cur_task_name);
					strcat(output_str, "=");
					strcat(output_str, person_name);
					strcat(output_str, "|");
					strcat(output_str, cur_task_name);
					strcat(output_str, "日期=");

					char date_buf[20] = "";
					sprintf( date_buf, "%4d-%02d-%02d", decision_date.year, decision_date.month+1, decision_date.day );
					strcat( output_str, date_buf );
				}
			}
		}
	}

	return ITK_ok;
}


int ORIGIN_EPM_auto_signoff(EPM_action_message_t msg)
{
	char *arg = 0;
	char *name = NULL, *value = NULL, buf[BUFSIZ] = "";
	logical hasDemoteNode = FALSE;
	char* task_name = NULL;
	char sign_info[1024] = "";
	char* root_task_name = NULL;
	tag_t current_task = msg.task;

	int sub_task_count = 0;
	tag_t root_task = NULLTAG, *sub_tasks = NULL;
	ITKCALL( EPM_ask_root_task( msg.task, &root_task ) );
	ITKCALL( EPM_ask_sub_tasks( root_task, &sub_task_count, &sub_tasks) );
	ITKCALL( EPM_ask_name2( root_task, &root_task_name ) );
	printf("root_task_name=%s\n",root_task_name);
	ITKCALL( EPM_ask_name2( current_task, &task_name ) );
	printf( "sub_task_count = %d\n", sub_task_count );
 
	if(strcmp(task_name,"编制")==0)
	{
		printf("task_name=%s\n",task_name);
        int s;
        EPM_decision_t decision = EPM_nogo;
 
        tag_t aUserTag, responsibleParty;

		s = EPM_ask_responsible_party(current_task, &responsibleParty);
        if (responsibleParty!= NULLTAG)
        {
            decision = EPM_go;
        } 
		char* userName;		
        s = POM_get_user (&userName, &aUserTag);
		date_t decision_date;
		char* person_name = NULL;
		ITKCALL(SA_ask_user_person_name2(aUserTag, &person_name));
		strcat(sign_info, "|");
		strcat(sign_info, task_name);
		strcat(sign_info, "=");
		strcat(sign_info, person_name);
		strcat(sign_info, "|");
		strcat(sign_info, task_name);
		strcat(sign_info, "日期=");
		char*   prop_name="last_mod_date";//或date_released
		
		ITKCALL(AOM_ask_value_date(current_task,prop_name,&decision_date));
		char date_buf[20] = "";
		sprintf( date_buf, "%4d-%02d-%02d", decision_date.year, decision_date.month+1, decision_date.day );
		strcat( sign_info, date_buf );
		printf(sign_info);
		printf("\n");
		MEM_free (userName); 

	}
	else
	{
	for ( int i=0; i<sub_task_count; i++ )
	{
		tag_t cur_task = sub_tasks[i];

		char* task_type = NULL;
		ITKCALL( WSOM_ask_object_type2( cur_task, &task_type ) );
		printf("current task type = %s\n", task_type);
		
		if ( stricmp( task_type, "EPMReviewTask" ) == 0 
			|| stricmp( task_type, "EPMDoTask" )==0
			||stricmp( task_type, "EPMAcknowladgeTask" )==0)
		{
			ORIGIN_ask_sign_info( cur_task, sign_info, task_name); 
		}
	}
	}
	//ORIGIN_ask_sign_info( current_task, sign_info ); 
printf("sign_info = %s\n", sign_info );

	if(current_task != NULLTAG)
	{
		int occur_of_counts = 0;
		tag_t *taskAttches = NULL;
		char* tgt_type = NULL;

		ITKCALL(EPM_ask_attachments(root_task, EPM_target_attachment, &occur_of_counts, &taskAttches));
		fprintf( stdout, "%d target attachment found\n", occur_of_counts );
		printf( (buf) );
		
		for (int i = 0; i < occur_of_counts; i++)
		{
			ITKCALL(WSOM_ask_object_type2(taskAttches[i], &tgt_type));
			
			//if(IsItemRevisionType(tgt_type))
			{
				tag_t revision = taskAttches[i];
				
				tag_t relation_type;
				ITKCALL(GRM_find_relation_type(TC_specification_rtype, &relation_type));
				if(relation_type != NULLTAG)
				{
					int count;
					tag_t *secondary_objects;

					ITKCALL(GRM_list_secondary_objects_only(revision, relation_type, &count, &secondary_objects));

					for (int j = 0; j < count; j++)
					{
						tag_t is_dataset_type = NULLTAG;
						ITKCALL(WSOM_ask_object_type2(secondary_objects[j], &tgt_type)); 
						printf( "tgt_type = %s\n", tgt_type );
						ITKCALL(AE_find_datasettype2(tgt_type, &is_dataset_type));
						printf("\n000000000000\n");
						if(is_dataset_type != NULLTAG)
						{
							char local_path[MAX_PATH];
							//Sleep(1000);
							//char item_id[256]="";
							//tag_t item=NULLTAG;
							//ITKCALL( ITEM_ask_item_of_rev (revision,&item) );
							//ITKCALL( ITEM_ask_id (item, item_id) );
							//Origin_create_temp_file(sign_info, local_path);
							char *userId=NULL;
							ITKCALL(POM_get_user_id(&userId));
							Origin_create_temp_file(sign_info,userId, local_path);
							printf("local_path=%s\n",local_path);
							//if(strcmp(tgt_type, "MSExcel") == 0)
							if(strstr(tgt_type, "XYXLS") != NULL || strcmp(tgt_type, "MSExcel") == 0)
							{
								Origin_process_msexcel(secondary_objects[j], local_path);
							}
							//else if(strcmp(tgt_type, "MSWord") == 0)
							else if(strstr(tgt_type, "XYDOC") != NULL || strcmp(tgt_type, "MSWord") == 0)
							{
								Origin_process_msword(secondary_objects[j], local_path);
							}
							else if(stricmp(tgt_type, "UGMASTER") == 0 )
							{
								Origin_process_ugmaster(secondary_objects[j], local_path);
							}
							else if(stricmp(tgt_type, "ACADDWG") == 0 )
							{
								Origin_process_acad(secondary_objects[j], local_path);
							}

							//break;
						}
					}

					MEM_free(secondary_objects);
				}
			}
		}

	}

	return ITK_ok;
}
int Origin_process_msexcel(tag_t dataset, char *dat_file)
{
	tag_t spec_dataset_rev = NULLTAG,
		ref_object = NULLTAG;

	AE_reference_type_t reference_type;

	ITKCALL(AE_ask_dataset_latest_rev(dataset, &spec_dataset_rev));

	char ref_name[WSO_name_size_c + 1] = "excel";
	ITKCALL(AE_ask_dataset_named_ref2(spec_dataset_rev, ref_name, &reference_type, &ref_object));
	if(reference_type == AE_PART_OF)
	{
		char* pathname = NULL;
		ITKCALL(IMF_ask_file_pathname2(ref_object, SS_WNT_MACHINE, &pathname));
		char* origin_file_name = NULL;
		ITKCALL(IMF_ask_original_file_name2(ref_object, &origin_file_name));

		char new_ds_name[WSO_name_size_c + 1] = "";
		char *new_file_name = USER_new_file_name(new_ds_name, ref_name, "xls", 0);
		char *temp_dir = getenv("temp");
		char temp_file[SS_MAXPATHLEN] = "";
		strcpy(temp_file, temp_dir);
		strcat(temp_file, "\\");
		strcat(temp_file, new_file_name);

		ITKCALL(IMF_export_file(ref_object, temp_file));


		int   iCnt;
		char *user_lib_env,pTempStr[500];
		char local_path[MAX_PATH] = "";
		char cmd[256] = "";
		/*user_lib_env = getenv("TC_USER_LIB"); 
		strcpy(local_path, user_lib_env);

		iCnt = strlen(user_lib_env);
		while( user_lib_env[iCnt] != '\\' )
		{
			iCnt--;
		}

		strcpy(pTempStr,"");
		strcpy(pTempStr,&user_lib_env[iCnt+1]);

		if( strcmp(pTempStr,"SubstMacros-MSExcel.wsf") != 0 )
		{
			strcat( user_lib_env, "\\SubstMacros-MSExcel.wsf" ); 
		}*/

		//strcpy( cmd, user_lib_env );
		strcpy( cmd, "SubstMacros-MSExcel.wsf" );
		strcat( cmd, " \"" ); 
		strcat( cmd, temp_file ); 
		strcat( cmd, "\" \"" );
		strcat( cmd, dat_file ); 
		strcat( cmd, "\"" );
		printf( "\n%s\n",cmd );
		system( cmd );

		//strcpy(user_lib_env, local_path);


		tag_t new_file_tag = NULLTAG;
		IMF_file_t file_descriptor;	
		ITKCALL(IMF_import_file(temp_file, new_file_name, SS_BINARY, &new_file_tag, &file_descriptor));
		ITKCALL(IMF_set_original_file_name2(new_file_tag, origin_file_name));
		ITKCALL(IMF_close_file(file_descriptor));
		ITKCALL(AOM_save(new_file_tag));
		ITKCALL(AOM_unlock(new_file_tag));

		ITKCALL(AOM_lock(spec_dataset_rev));
		//ITKCALL(AOM_load (dataset));
		//ITKCALL(AOM_load(spec_dataset_rev));
		ITKCALL(AE_remove_dataset_named_ref_by_tag2(spec_dataset_rev, ref_name, ref_object));
		ITKCALL(AE_add_dataset_named_ref2(spec_dataset_rev, ref_name, AE_PART_OF, new_file_tag));
		ITKCALL(AOM_save(spec_dataset_rev));
		ITKCALL(AOM_unlock(spec_dataset_rev));
	}

	return ITK_ok;
}

int Origin_process_msword(tag_t dataset, char *dat_file)
{
	tag_t spec_dataset_rev = NULLTAG,
		ref_object = NULLTAG;

	AE_reference_type_t reference_type;
	//printf("\n111111111111\n");
	ITKCALL(AE_ask_dataset_latest_rev(dataset, &spec_dataset_rev));

	char ref_name[WSO_name_size_c + 1] = "word";
	//printf("\n22222222222\n");
	ITKCALL(AE_ask_dataset_named_ref2(spec_dataset_rev, ref_name, &reference_type, &ref_object));
	if(reference_type == AE_PART_OF)
	{
		//printf("\n3333333333333\n");
		char* pathname = NULL;
		ITKCALL(IMF_ask_file_pathname2(ref_object, SS_WNT_MACHINE, &pathname));
		char* origin_file_name = NULL;
		ITKCALL(IMF_ask_original_file_name2(ref_object, &origin_file_name));
		//printf("\n44444444444444\n");
		char new_ds_name[WSO_name_size_c + 1] = "";
		char *new_file_name = USER_new_file_name(new_ds_name, ref_name, "doc", 0);
		char *temp_dir = getenv("temp");
		//printf("\n555555555555\n");
		char temp_file[SS_MAXPATHLEN] = "";
		strcpy(temp_file, temp_dir);
		strcat(temp_file, "\\");
		strcat(temp_file, new_file_name);

		ITKCALL(IMF_export_file(ref_object, temp_file));
		//printf("\n66666666666666\n");

		int   iCnt;
		char *user_lib_env,pTempStr[500];
		char local_path[MAX_PATH] = "";
		char cmd[256] = "";
		//user_lib_env = getenv("TC_USER_LIB"); 
		//strcpy(local_path, user_lib_env);

		//iCnt = strlen(user_lib_env);
		//while( user_lib_env[iCnt] != '\\' )
		//{
		//	iCnt--;
		//}

		//strcpy(pTempStr,"");
		//strcpy(pTempStr,&user_lib_env[iCnt+1]);

		//if( strcmp(pTempStr,"SubstMacros-MSWord.wsf") != 0 )
		//{
		//	strcat( user_lib_env, "\\SubstMacros-MSWord.wsf" ); 
		//}

		//strcpy( cmd, user_lib_env );
		strcpy( cmd, "SubstMacros-MSWord.wsf" );
		strcat( cmd, " \"" ); 
		strcat( cmd, temp_file ); 
		strcat( cmd, "\" \"" );
		strcat( cmd, dat_file ); 
		strcat( cmd, "\"" );
		printf( "\n%s\n",cmd );
		system( cmd );
//printf("\n777777777777\n");
		//strcpy(user_lib_env, local_path);


		tag_t new_file_tag = NULLTAG;
		IMF_file_t file_descriptor;	
		ITKCALL(IMF_import_file(temp_file, new_file_name, SS_BINARY, &new_file_tag, &file_descriptor));
		ITKCALL(IMF_set_original_file_name2(new_file_tag, origin_file_name));
		ITKCALL(IMF_close_file(file_descriptor));
		ITKCALL(AOM_save(new_file_tag));
		ITKCALL(AOM_unlock(new_file_tag));

		ITKCALL(AOM_lock(spec_dataset_rev));
		//ITKCALL(AOM_load (dataset));
		//ITKCALL(AOM_load (spec_dataset_rev));
		//ITKCALL(AOM_lock(spec_dataset_rev));
		ITKCALL(AE_remove_dataset_named_ref_by_tag2(spec_dataset_rev, ref_name, ref_object));
		//ITKCALL(AE_replace_dataset_named_ref(spec_dataset_rev,ref_object,ref_name,AE_PART_OF,new_file_tag));
		ITKCALL(AE_add_dataset_named_ref2(spec_dataset_rev, ref_name, AE_PART_OF, new_file_tag));
		ITKCALL(AOM_save(spec_dataset_rev));
		ITKCALL(AOM_unlock(spec_dataset_rev));
	}

	return ITK_ok;
}

int Origin_process_acad(tag_t dataset, char *dat_file)
{
	tag_t spec_dataset_rev = NULLTAG,
		ref_object = NULLTAG;

	AE_reference_type_t reference_type;
		
	ITKCALL(AE_ask_dataset_latest_rev(dataset, &spec_dataset_rev));

	char ref_name[WSO_name_size_c + 1] = "dwg";
	printf("\n1111111\n");
	ITKCALL(AE_ask_dataset_named_ref2(spec_dataset_rev, ref_name, &reference_type, &ref_object));
	if(ref_object==NULLTAG)
	{
		printf("\nref_object is NULLTAG\n");
		return ITK_ok;
	}
	printf("\reference_type=%d\n",reference_type);
	if(reference_type == AE_PART_OF)
	{
		char* pathname = NULL;
		printf("\n3333333\n");
		ITKCALL(IMF_ask_file_pathname2(ref_object, SS_WNT_MACHINE, &pathname));
		char* origin_file_name = NULL;
		ITKCALL(IMF_ask_original_file_name2(ref_object, &origin_file_name));

		char new_ds_name[WSO_name_size_c + 1] = "";
		char *new_file_name = USER_new_file_name(new_ds_name, ref_name, "dwg", 0);
		char *temp_dir = getenv("TEMP");
		char temp_file[SS_MAXPATHLEN] = "";
		strcpy(temp_file, temp_dir);
		strcat(temp_file, "\\");
		strcat(temp_file, new_file_name);
		printf("\ntemp_file=%s\n",temp_file);

		ITKCALL(IMF_export_file(ref_object, temp_file));
		printf("\n555555555\n");

		int   iCnt;
		char *user_lib_env,pTempStr[500];
		char local_path[MAX_PATH] = "";
		char cmd[256] = "";
		//user_lib_env = getenv("TC_USER_LIB"); 
		//strcpy(local_path, user_lib_env);

		//iCnt = strlen(user_lib_env);
		//while( user_lib_env[iCnt] != '\\' )
		//{
		//	iCnt--;
		//}

		//strcpy(pTempStr,"");
		//strcpy(pTempStr,&user_lib_env[iCnt+1]);

		//if( strcmp(pTempStr,"acad_signoff.exe") != 0 )
		//{
		//	strcat( user_lib_env, "\\acad_signoff.exe" ); 
		//}

		//strcpy( cmd, user_lib_env );
		//strcat( user_lib_env, "\\acad_signoff.exe" ); 
		strcpy( cmd, "acad_signoff.exe" );
		strcat( cmd, "   \"" ); 
		strcat( cmd, temp_file ); 
		strcat( cmd, "\"   \"" );
		strcat( cmd, dat_file ); 
		strcat( cmd, "\"" );
		printf( "\n%s\n",cmd );
		system( cmd );

		//strcpy(user_lib_env, local_path);


		tag_t new_file_tag = NULLTAG;
		IMF_file_t file_descriptor;	
		ITKCALL(IMF_import_file(temp_file, new_file_name, SS_BINARY, &new_file_tag, &file_descriptor));
		ITKCALL(IMF_set_original_file_name2(new_file_tag, origin_file_name));
		ITKCALL(IMF_close_file(file_descriptor));
		ITKCALL(AOM_save(new_file_tag));
		ITKCALL(AOM_unlock(new_file_tag));

		ITKCALL(AOM_lock(spec_dataset_rev));
		//ITKCALL(AOM_load (spec_dataset_rev));
		ITKCALL(AE_remove_dataset_named_ref_by_tag2(spec_dataset_rev, ref_name, ref_object));
		ITKCALL(AE_add_dataset_named_ref2(spec_dataset_rev, ref_name, AE_PART_OF, new_file_tag));
		ITKCALL(AOM_save(spec_dataset_rev));
		ITKCALL(AOM_unlock(spec_dataset_rev));
	}

	return ITK_ok;
}
int Origin_process_ugmaster( tag_t dataset, char *dat_file)
{
	tag_t spec_dataset_rev = NULLTAG,
		ref_object = NULLTAG;

	AE_reference_type_t reference_type;

	ITKCALL(AE_ask_dataset_latest_rev(dataset, &spec_dataset_rev));

	char* ref_name = "UGPART";
	ITKCALL(AE_ask_dataset_named_ref2(spec_dataset_rev, ref_name, &reference_type, &ref_object));
	if(reference_type == AE_PART_OF)
	{
		char* pathname = NULL;
		ITKCALL(IMF_ask_file_pathname2(ref_object, SS_WNT_MACHINE, &pathname));
		char* origin_file_name = NULL;
		ITKCALL(IMF_ask_original_file_name2(ref_object, &origin_file_name));

		char new_ds_name[WSO_name_size_c + 1] = "";
		char *new_file_name = USER_new_file_name(new_ds_name, ref_name, "prt", 0);
		char *temp_dir = getenv("temp");
		char temp_file[SS_MAXPATHLEN] = "";
		strcpy(temp_file, temp_dir);
		strcat(temp_file, "\\");
		strcat(temp_file, new_file_name);

		ITKCALL(IMF_export_file(ref_object, temp_file));
		
		string old_bin_path(getenv("PATH"));
		{
			char *ug_base_dir = getenv("UGII_ROOT_DIR");
			string new_bin_path("PATH=");
			if ( ug_base_dir && *ug_base_dir )
			{
				new_bin_path.append(ug_base_dir).append(";");
			}

			new_bin_path.append(old_bin_path);
			putenv(new_bin_path.c_str());
		}

		char *user_lib_env = getenv("TC_USER_LIB");
		string info_cmd(user_lib_env);
		info_cmd.append("\\").append("nx2d_signoff");
		info_cmd.append(" -file=\"").append(temp_file).append("\"");
		info_cmd.append(" -dat=\"").append(dat_file).append("\"");
		
		system(info_cmd.c_str());

		{
			putenv(old_bin_path.c_str());
		}

		tag_t new_file_tag = NULLTAG;
		IMF_file_t file_descriptor;	
		ITKCALL(IMF_import_file(temp_file, new_file_name, SS_BINARY, &new_file_tag, &file_descriptor));
		ITKCALL(IMF_set_original_file_name2(new_file_tag, origin_file_name));
		ITKCALL(IMF_close_file(file_descriptor));
		ITKCALL(AOM_save(new_file_tag));
		ITKCALL(AOM_unlock(new_file_tag));

		//ITKCALL(AOM_lock(spec_dataset_rev));
		ITKCALL(AE_remove_dataset_named_ref_by_tag2(spec_dataset_rev, ref_name, ref_object));
		ITKCALL(AE_add_dataset_named_ref2(spec_dataset_rev, ref_name, AE_PART_OF, new_file_tag));
		ITKCALL(AOM_save(spec_dataset_rev));
		ITKCALL(AOM_unlock(spec_dataset_rev));
	}

	return ITK_ok;
}


int Origin_process_proe(tag_t dataset, char *dat_file)
{
	tag_t spec_dataset_rev = NULLTAG,ds_type=NULLTAG,*ref_objects=NULL,
		ref_object = NULLTAG;
	int	ref_count=0,n=0,ifail=0;
	char **ref_names=NULL;
	AE_reference_type_t reference_type;

	ITKCALL(AE_ask_dataset_latest_rev(dataset, &spec_dataset_rev));
	printf("11111111111\n");
	char* ref_name = NULL;//"ACADDWG";
	ITKCALL(AE_ask_dataset_datasettype (dataset,&ds_type));
	ITKCALL(AE_ask_datasettype_refs(ds_type,&ref_count,&ref_names));
	for (int index = 0; index < ref_count; index++)
	{
		sprintf(ref_name,"%s",ref_names[index]);
	}
	printf("2222222222\n");
	ITKCALL( AE_ask_dataset_named_refs( dataset, &ref_count, &ref_objects ) );
	for(n = 0;n<ref_count;n++)
	{
		//ITKCALL(AE_ask_dataset_named_ref(dataset, ref_name, &reference_type, &ref_object));
		ITKCALL( AE_find_dataset_named_ref2( dataset,n, &ref_name, &reference_type, &ref_object ) )
		if(reference_type == AE_PART_OF)
		{
			char pathname[SS_MAXPATHLEN] = "";
			//ITKCALL(IMF_ask_file_pathname(ref_object, SS_WNT_MACHINE, pathname));
			char* origin_file_name = NULL;
			printf("333333333\n");
			ITKCALL(IMF_ask_original_file_name2(ref_objects[n], &origin_file_name));
			printf("origin_file_name=%s\n",origin_file_name);

			char new_ds_name[WSO_name_size_c + 1] = "";
			char *ext = strrchr(origin_file_name,'.') + 1;
			//char *ext = strchr(origin_file_name,'.') + 1;
			char *new_file_name = USER_new_file_name(new_ds_name, ref_name, ext, 0);
			char *temp_dir = getenv("TEMP");
			char temp_file[SS_MAXPATHLEN] = "";
			char new_prt_name[256]="";
			char cmd[512] = "";
			//strcpy(temp_file, temp_dir);
			strcpy(temp_file, "c:\\temp");
			strcat(temp_file, "\\");
			strcat(temp_file, new_file_name);
			strcpy(new_prt_name,"c:\\temp");
			strcat(new_prt_name,"\\");
			strcat(new_prt_name,origin_file_name);
			printf("new_file_name=%s\n",temp_file);
			printf("444444444\n");
			if(remove(new_prt_name)!=0)
			{
				printf("\n remove %s failed\n",new_prt_name);
				sprintf(cmd,"attrib -r %s",new_prt_name);
				ifail = system(cmd);
				Sleep(3000);
				if(ifail==0)
				{}
				if(remove(new_prt_name)!=0)
					printf("\n readonly attribute,delete failed!\n");
			}
			ITKCALL(IMF_export_file(ref_objects[n], new_prt_name));
			printf("55555555\n");
			if(rename(temp_file,new_prt_name)!=0)
				printf("\n rename %s to %s failed!\n",temp_file,new_prt_name);
			//rename(temp_file,new_prt_name);

			//int   iCnt;
			//char *user_lib_env,pTempStr[500];
			//char local_path[MAX_PATH] = "";
			
			//user_lib_env = getenv("TC_USER_LIB"); 
			//strcpy(local_path, user_lib_env);

			//iCnt = strlen(user_lib_env);
			//while( user_lib_env[iCnt] != '\\' )
			//{
			//	iCnt--;
			//}

			//strcpy(pTempStr,"");
			//strcpy(pTempStr,&user_lib_env[iCnt+1]);

			//if( strcmp(pTempStr,"SignoffProe.exe") != 0 )
			//{
			//	strcat( user_lib_env, "\\SignoffProe.exe" ); 
			//}
			printf("666666666\n");
			//strcpy( cmd, user_lib_env );
			//strcat( cmd, " \"" ); 
			//strcat( cmd, temp_file ); 
			//strcat( cmd, "\" \"" );
			//strcat( cmd, dat_file ); 
			//strcat( cmd, "\"" );
			strcpy( cmd, "c:\\temp\\SignoffProe.exe" );
			strcat( cmd, " \"" ); 
			strcat( cmd, new_prt_name ); 
			strcat( cmd, "\" \"" );
			strcat( cmd, dat_file ); 
			strcat( cmd, "\"" );
			printf( "\n%s\n",cmd );
			system( cmd );

			//strcpy(user_lib_env, local_path);

			//FILE *fp;
			//char env[256]="";
			//char buf[1024];
			//sprintf(env,"%s\\version.txt",getenv("TEMP"));
			//if((fp=fopen(env,"r"))!=NULL)
			//{
			//	fgets(buf,1024,fp);
			//	fclose(fp);
			//}

			tag_t new_file_tag = NULLTAG;
			IMF_file_t file_descriptor;	
			ITKCALL(IMF_import_file(new_prt_name, new_file_name, SS_BINARY, &new_file_tag, &file_descriptor));
			ITKCALL(IMF_set_original_file_name2(new_file_tag, origin_file_name));
			ITKCALL(IMF_close_file(file_descriptor));
			ITKCALL(AOM_save(new_file_tag));
			ITKCALL(AOM_unlock(new_file_tag));

			//ITKCALL(AOM_lock(dataset));
			ITKCALL(AE_remove_dataset_named_ref_by_tag2(dataset, ref_name, ref_objects[n]));
			ITKCALL(AE_add_dataset_named_ref2(dataset, ref_name, AE_PART_OF, new_file_tag));
			ITKCALL(AOM_save(dataset));
			ITKCALL(AOM_unlock(dataset));
		}
	}

	return ITK_ok;
}
//int Origin_create_temp_file(char *file_content, char *file_name)
//{
//	time_t now;
//	struct tm *p;
//	FILE *filePtr = NULL;
//
//	char *temp_dir = getenv("TEMP");
//	char local_path[MAX_PATH] = "";
//
//	time(&now);
//	p = localtime(&now);
//	if(temp_dir[strlen(temp_dir) - 1] == '\\')
//	{
//		sprintf_s(local_path, "%s%d-%d-%d-%d-%d-%d.dat", temp_dir,
//			1900+p->tm_year,p->tm_mon+1 ,p->tm_mday+1 ,p->tm_hour,p->tm_min ,p->tm_sec);
//	}
//	else
//	{
//		sprintf_s(local_path, "%s\\%d-%d-%d-%d-%d-%d.dat", temp_dir,
//			1900+p->tm_year,p->tm_mon+1 ,p->tm_mday+1 ,p->tm_hour,p->tm_min ,p->tm_sec);
//	}
//
//	if((filePtr = fopen(local_path, "wt")) == NULL)
//	{
//		printf("Can not create the temp dat file!\n");
//		return -1;
//	}
//
//	strcpy(file_name, local_path);
//
//	fwrite(file_content, sizeof(char), strlen(file_content), filePtr);
//	fclose(filePtr);
//
//	return ITK_ok;
//}
int Origin_create_temp_file(char *file_content,char *item_id, char *file_name)
{
	time_t now;
	struct tm *p;
	FILE *filePtr = NULL;

	char *temp_dir = getenv("TEMP");
	char local_path[MAX_PATH] = "";

	time(&now);
	p = localtime(&now);
	if(temp_dir[strlen(temp_dir) - 1] == '\\')
	{
		sprintf_s(local_path, "%s%s%d-%d-%d-%d-%d-%d.dat", temp_dir,item_id,
			1900+p->tm_year,p->tm_mon+1 ,p->tm_mday+1 ,p->tm_hour,p->tm_min ,p->tm_sec);
	}
	else
	{
		sprintf_s(local_path, "%s\\%s%d-%d-%d-%d-%d-%d.dat", temp_dir,item_id,
			1900+p->tm_year,p->tm_mon+1 ,p->tm_mday+1 ,p->tm_hour,p->tm_min ,p->tm_sec);
	}

	if((filePtr = fopen(local_path, "wt")) == NULL)
	{
		printf("Can not create the temp dat file!\n");
		return -1;
	}

	strcpy(file_name, local_path);

	fwrite(file_content, sizeof(char), strlen(file_content), filePtr);
	fclose(filePtr);

	return ITK_ok;
}


void TC2C(const PTCHAR tc, char * c) 
{ 
	#if defined(UNICODE) 
	WideCharToMultiByte(CP_ACP, 0, tc, -1, c, wcslen(tc), 0, 0); 
	c[wcslen(tc)] = 0; 
	#else 
	lstrcpy((PTSTR)c, (PTSTR)tc); 
	#endif 
}
int ORIGIN_EPM_signoff_itemid(EPM_action_message_t msg)
{
	char *arg = 0;
	char *name = NULL, *value = NULL, buf[BUFSIZ] = "";
	logical hasDemoteNode = FALSE;
	char* task_name = NULL;
	char sign_info[1024] = "";
	char* root_task_name = NULL;

	tag_t current_task = msg.task;
//============================================================
	map<string,string> item_types_map;
	map<string,string>::iterator item_types_map_iter = item_types_map.begin();
	logical is_debug = FALSE;
	char value_name[128]="";
	char fixed_prop[WSO_name_size_c + 1]="";
	char  *flag = NULL;
	int sub_task_count = 0;

	POM_AM__set_application_bypass(TRUE);
	int num_of_arguments = TC_number_of_arguments( msg.arguments );

	for (int i=0; i<num_of_arguments; i++)
	{
		ITKCALL( ITK_ask_argument_named_value( TC_next_argument( msg.arguments ), &flag, &value ) );
		if ( strcmp( flag, "type" ) == 0 && value != NULL )
		{
			vector<string> ans;
			Split( value, ',', ans );
			for (size_t j=0; j<ans.size(); j++)
			{
				item_types_map.insert(make_pair(ans[j],ans[j]));		
			}
		}
		else if ( strcmp( flag, "target_value" ) == 0 && value != NULL )
		{
			strcpy( value_name,value );
			//sprintf(value_name,"%s",value);
			printf("target_value=%s\n",value_name);
		}
		MEM_free( flag );
		MEM_free( value );
	}
	if (strlen(value_name)==0 )
	{
		printf( ("target_value argument is not valid, please check!\n") );
		return EPM_missing_req_arg;
	}
//===========================================================
	tag_t root_task = NULLTAG, *sub_tasks = NULL;
	ITKCALL( EPM_ask_root_task( msg.task, &root_task ) );
	ITKCALL( EPM_ask_sub_tasks( root_task, &sub_task_count, &sub_tasks) ); 
	ITKCALL( EPM_ask_name2( root_task, &root_task_name ) );
	printf("root_task_name=%s\n",root_task_name);
	ITKCALL( EPM_ask_name2( current_task, &task_name ) );
	printf( "sub_task_count = %d\n", sub_task_count );
 
	if(strcmp(task_name,"编制")==0)
	{
		printf("task_name=%s\n",task_name);
        int s;
        EPM_decision_t decision = EPM_nogo;
 
        tag_t aUserTag, responsibleParty;

		s = EPM_ask_responsible_party(current_task, &responsibleParty);
        if (responsibleParty!= NULLTAG)
        {
            decision = EPM_go;
        } 
		char* userName;		
        s = POM_get_user (&userName, &aUserTag);
		date_t decision_date;
		char* person_name = NULL;
		ITKCALL(SA_ask_user_person_name2(aUserTag, &person_name));
		strcat(sign_info, "|");
		strcat(sign_info, task_name);
		strcat(sign_info, "=");
		strcat(sign_info, person_name);
		strcat(sign_info, "|");
		strcat(sign_info, task_name);
		strcat(sign_info, "日期=");
		char*   prop_name="last_mod_date";//或date_released
		
		ITKCALL(AOM_ask_value_date(current_task,prop_name,&decision_date));
		char date_buf[20] = "";
		sprintf( date_buf, "%4d-%02d-%02d", decision_date.year, decision_date.month+1, decision_date.day );
		strcat( sign_info, date_buf );
		printf(sign_info);
		printf("\n");
		MEM_free (userName); 

	}
	else
	{
		for ( int i=0; i<sub_task_count; i++ )
		{
			tag_t cur_task = sub_tasks[i];

			char* task_type = NULL;
			ITKCALL( WSOM_ask_object_type2( cur_task, &task_type ) );
			printf("current task type = %s\n", task_type);
			
			if ( stricmp( task_type, "EPMReviewTask" ) == 0 
				|| stricmp( task_type, "EPMDoTask" )==0
				||stricmp( task_type, "EPMAcknowladgeTask" )==0)
			{
				ORIGIN_ask_sign_info( cur_task, sign_info, task_name); 
			}
		}
	}
	//ORIGIN_ask_sign_info( current_task, sign_info ); 
printf("sign_info = %s\n", sign_info );
printf("value_name=%s\n",value_name);
	if(current_task != NULLTAG)
	{
		int occur_of_counts = 0;
		tag_t *taskAttches = NULL;
		char* tgt_type = NULL;

		ITKCALL(EPM_ask_attachments(root_task, EPM_target_attachment, &occur_of_counts, &taskAttches));
		fprintf( stdout, "%d target attachment found\n", occur_of_counts );
		printf( (buf) );
		
		for (int i = 0; i < occur_of_counts; i++)
		{
			ITKCALL(WSOM_ask_object_type2(taskAttches[i], &tgt_type));
			
			//if(IsItemRevisionType(tgt_type))
			{
				tag_t revision = taskAttches[i];
				
				tag_t relation_type;
				ITKCALL(GRM_find_relation_type(TC_specification_rtype, &relation_type));
				if(relation_type != NULLTAG)
				{
					int count;
					tag_t *secondary_objects;

					ITKCALL(GRM_list_secondary_objects_only(revision, relation_type, &count, &secondary_objects));

					for (int j = 0; j < count; j++)
					{
						tag_t is_dataset_type = NULLTAG;
						ITKCALL(WSOM_ask_object_type2(secondary_objects[j], &tgt_type)); 
						printf( "tgt_type = %s\n", tgt_type );
						ITKCALL(AE_find_datasettype2(tgt_type, &is_dataset_type));
						printf("value_name=%s\n",value_name);
						if(is_dataset_type != NULLTAG)
						{
							char local_path[MAX_PATH];
							//Sleep(1000);
							char* item_id = NULL,info[512]="";
							tag_t item=NULLTAG;
							ITKCALL( ITEM_ask_item_of_rev (revision,&item) );
							ITKCALL( ITEM_ask_id2 (item, &item_id) );
							//Origin_create_temp_file(sign_info, local_path);
							char *userId=NULL;
							printf("value_name=%s\n",value_name);
							sprintf(info,"|%s=%s\n",value_name,item_id);
							printf("info=%s\n",info);
							ITKCALL(POM_get_user_id(&userId));
							Origin_create_temp_file(info,userId, local_path);
							printf("local_path=%s\n",local_path);
							//if(strcmp(tgt_type, "MSExcel") == 0)
							if(strstr(tgt_type, "XYXLS") != NULL || strcmp(tgt_type, "MSExcel") == 0)
							{
								Origin_process_msexcel(secondary_objects[j], local_path);
							}
							//else if(strcmp(tgt_type, "MSWord") == 0)
							else if(strstr(tgt_type, "XYDOC") != NULL || strcmp(tgt_type, "MSWord") == 0)
							{
								Origin_process_msword(secondary_objects[j], local_path);
							}
							else if(stricmp(tgt_type, "UGMASTER") == 0 )
							{
								Origin_process_ugmaster(secondary_objects[j], local_path);
							}
							else if(stricmp(tgt_type, "ACADDWG") == 0 )
							{
								Origin_process_acad(secondary_objects[j], local_path);
							}

							//break;
						}
					}

					MEM_free(secondary_objects);
				}
			}
		}

	}

	return ITK_ok;
}
int ORIGIN_EPM_dwg_to_pdf(EPM_action_message_t msg)
{
	//获得我的文档路径
    //char m_lpszDefaultDir[MAX_PATH];
    //char   szDocument[MAX_PATH]={0};   
    //memset(m_lpszDefaultDir,0,_MAX_PATH);   
	LPWSTR m_lpszDefaultDir,szDocument;
	char mydoc_path[512]="";
    LPITEMIDLIST pidl=NULL;   
    SHGetSpecialFolderLocation(NULL,   CSIDL_PERSONAL,   &pidl);   
    if   (pidl   &&   SHGetPathFromIDList(pidl,   szDocument))   
    {   
        //GetShortPathName(szDocument,m_lpszDefaultDir,_MAX_PATH);   
		//GetLongPathName(szDocument,m_lpszDefaultDir,_MAX_PATH);  
	//TC2C(m_lpszDefaultDir,mydoc_path);
		TC2C(szDocument,mydoc_path);
	}


	int ifail = ITK_ok, i = 0, j = 0, num_of_args = 0;

	char *flag = NULL;                    /* FREE THIS VARIABLE WITH MEM_FREE */
	char *value = NULL;                   /* FREE THIS VARIABLE WITH MEM_FREE */
	char *normal_value = NULL;            /* FREE THIS VARIABLE WITH MEM_FREE */

	char 
		dsType[WSO_name_size_c + 1] = "",
		refName[WSO_name_size_c + 1] = "",
		extraParams[WSO_name_size_c + 1] = "",
		obj_class_name[WSO_name_size_c+1] = "",
		obj_type_name[WSO_name_size_c+1] = "";
	tag_t 
		job = NULLTAG,
		root_task = NULLTAG,
		*tasks = NULL,
		//responsible_party = NULLTAG,
		msg_ds_tag = NULLTAG,
		*attach_object_tags = NULL,
		attach_object_tag = NULLTAG,
		iman_type_tag = NULLTAG;
	int 	
		count  = 0,
		attach_object_count = 0;
	char 
		object_type[WSO_name_size_c+1] = "",
		task_name[WSO_name_size_c + 1] = "",
		osUserName[SA_name_size_c + 1] = "",
		root_task_name[WSO_name_size_c +1] = "";
	WSO_description_t  description;
	char line_attrs[20000] = "";

	char mySub[20] = "", *myPtr = NULL;

	char *temp_dir = getenv("TEMP");
	char local_path[MAX_PATH] = "",
		buf[128]="";
	
	ITKCALL( EPM_ask_job( msg.task, &job ) );
	ITKCALL( EPM_ask_root_task( job, &root_task ) );	
	ITKCALL( EPM_ask_sub_tasks( root_task, &count, &tasks ) );

	int occur_of_counts = 0;
	tag_t *taskAttches = NULL;
	char* tgt_type = NULL;

	ITKCALL(EPM_ask_attachments(root_task, EPM_target_attachment, &occur_of_counts, &taskAttches));
	fprintf( stdout, "%d target attachment found\n", occur_of_counts );
	printf( (buf) );

	for (int i = 0; i < occur_of_counts; i++)
	{
		ITKCALL(WSOM_ask_object_type2(taskAttches[i], &tgt_type));
		
		if(IsItemRevisionType(tgt_type))
		{
			tag_t revision = taskAttches[i];
			
			tag_t relation_type;
			ITKCALL(GRM_find_relation_type(TC_specification_rtype, &relation_type));
			if(relation_type != NULLTAG)
			{
				int count;
				tag_t *secondary_objects;

				ITKCALL(GRM_list_secondary_objects_only(revision, relation_type, &count, &secondary_objects));
				for (int j = 0; j < count; j++)
				{
					tag_t is_dataset_type = NULLTAG;
					ITKCALL(WSOM_ask_object_type2(secondary_objects[j], &tgt_type)); printf( "tgt_type = %s\n", tgt_type );
					ITKCALL(AE_find_datasettype2(tgt_type, &is_dataset_type));
					if(is_dataset_type != NULLTAG)
					{
						char local_path[MAX_PATH];
						//Origin_create_temp_file(sign_info, local_path);

						if(stricmp(tgt_type, "ACADDWG") == 0 )
						{
							tag_t spec_dataset_rev = NULLTAG,
								ref_object = NULLTAG;

							AE_reference_type_t reference_type;

							ITKCALL(AE_ask_dataset_latest_rev(secondary_objects[j], &spec_dataset_rev));

							char ref_name[WSO_name_size_c + 1] = "dwg";
							ITKCALL(AE_ask_dataset_named_ref2(spec_dataset_rev, ref_name, &reference_type, &ref_object));
							if(reference_type == AE_PART_OF)
							{
								char* pathname = NULL;
								ITKCALL(IMF_ask_file_pathname2(ref_object, SS_WNT_MACHINE, &pathname));
								char* origin_file_name = NULL;
								ITKCALL(IMF_ask_original_file_name2(ref_object, &origin_file_name));

								char new_ds_name[WSO_name_size_c + 1] = "";
								char *new_file_name = USER_new_file_name(new_ds_name, ref_name, "dwg", 0);
								char *temp_dir = getenv("TEMP");
								char temp_file[SS_MAXPATHLEN] = "";
								strcpy(temp_file, temp_dir);
								strcat(temp_file, "\\");
								//strcat(temp_file, new_file_name); // 这里
								strcat(temp_file, origin_file_name);

								ITKCALL(IMF_export_file(ref_object, temp_file));


								int   iCnt;
								char *user_lib_env,pTempStr[500];
								char local_path[MAX_PATH] = "";
								char cmd[256] = "";
								/*user_lib_env = getenv("TC_USER_LIB"); 
								strcpy(local_path, user_lib_env);

								iCnt = strlen(user_lib_env);
								while( user_lib_env[iCnt] != '\\' )
								{
									iCnt--;
								}

								strcpy(pTempStr,"");
								strcpy(pTempStr,&user_lib_env[iCnt+1]);

								if( strcmp(pTempStr,"acad_signoff.exe") != 0 )
								{
									strcat( user_lib_env, "\\acad_signoff.exe" ); 
								}*/

								//strcpy( cmd, user_lib_env );
								strcpy( cmd, "dwg2pdf.exe" );
								strcat( cmd, "   \"" ); 
								strcat( cmd, temp_file ); 
								//strcat( cmd, "\"   \"" );
								//strcat( cmd, dat_file ); 
								strcat( cmd, "\"" );
								printf( "\n%s\n",cmd );
								system( cmd );

								//strcpy(user_lib_env, local_path);

								//创建pdf数据集
								tag_t tool=NULLTAG, 
									datasettype=NULLTAG,
									parent_item_tag=NULLTAG,
									new_ds = NULLTAG;
								ITKCALL(AE_find_tool2 ("Adobe Acrobat",&tool));
								if(tool==NULLTAG)
									return 1;
								ITKCALL(AE_find_datasettype2 ("PDF",&datasettype));
								if(datasettype==NULLTAG)
									return 1;
								char* rev_id = NULL,
									ds_name[128]="",
									*item_id = NULL,
									*format_name = NULL;
								ITKCALL( ITEM_ask_rev_id2 (revision,&rev_id));
								ITKCALL( ITEM_ask_item_of_rev (revision, &parent_item_tag));
								ITKCALL( ITEM_ask_id2 (parent_item_tag,&item_id));
								sprintf(ds_name,"%s/%s",item_id,rev_id);
								ITKCALL( AE_create_dataset_with_id ( datasettype, ds_name, ds_name, item_id, rev_id, &new_ds ) );
								//ITKCALL( AE_ask_dataset_tool( ds_tag, &tool ) );
								ITKCALL( AE_set_dataset_tool( new_ds, tool  ) );
								ITKCALL( AE_ask_dataset_format2( spec_dataset_rev, &format_name ) );
								ITKCALL( AE_set_dataset_format2( new_ds, format_name ) );
								ITKCALL( AE_save_myself( new_ds ) );
								ITKCALL( AOM_unlock( new_ds ) );

								//添加命名引用
								char reference_name[AE_reference_size_c + 1] = "",
									destPathName[512]="";
			
								AE_reference_type_t reference_type;
								IMF_file_t new_file_descriptor,file_descriptor = NULL;
								tag_t new_file_tag=NULLTAG;
								//object_type[WSO_name_size_c + 1] = "",
								//strcat(mydoc_path,"\\");
								char *ptr=NULL;
								
								sprintf(destPathName,"%s\\%s",mydoc_path,origin_file_name);
								ptr = strrchr(destPathName,'.');
								strcpy(ptr,"\0");
								strcat(destPathName,".pdf");
								printf("\ndestPathName=%s\n",destPathName);
								char    *generatedFileName=NULL;
								generatedFileName = USER_new_file_name (ds_name, reference_name,"pdf", 0 );
								ITKCALL( IMF_import_file(destPathName,generatedFileName,SS_TEXT | SS_TRANSLATE | SS_BINARY ,&new_file_tag,&file_descriptor))
								ITKCALL( IMF_set_original_file_name2( new_file_tag, origin_file_name ) )
								ITKCALL( IMF_close_file( file_descriptor ) )
								ITKCALL( AOM_save (new_file_tag))
								tag_t	new_ds_rev=NULLTAG;
								ITKCALL(  AE_ask_dataset_latest_rev( new_ds, &new_ds_rev ) )
								//ITKCALL(  AOM_lock( new_ds_rev ) )
								printf("reference_name=%s\n",reference_name);
								printf("reference_type=%d\n",reference_type);
								//add name ref
								strcpy(reference_name,"PDF");
								ITKCALL( ifail = AE_add_dataset_named_ref2( new_ds_rev, reference_name, AE_PART_OF , new_file_tag ) )  
								printf("ifail of add_reference_name =%d\n",ifail);
								ITKCALL(AOM_save(new_ds_rev))                             
								ITKCALL( ifail = AOM_unlock( new_ds_rev ) )   



								//tag_t new_file_tag = NULLTAG;
								//IMF_file_t file_descriptor;	
								//ITKCALL(IMF_import_file(temp_file, new_file_name, SS_BINARY, &new_file_tag, &file_descriptor));
								//ITKCALL(IMF_set_original_file_name(new_file_tag, origin_file_name));
								//ITKCALL(IMF_close_file(file_descriptor));
								//ITKCALL(AOM_save(new_file_tag));
								//ITKCALL(AOM_unlock(new_file_tag));

								//ITKCALL(AOM_lock(spec_dataset_rev));
								//ITKCALL(AE_remove_dataset_named_ref_by_tag(spec_dataset_rev, ref_name, ref_object));
								//ITKCALL(AE_add_dataset_named_ref(spec_dataset_rev, ref_name, AE_PART_OF, new_file_tag));
								//ITKCALL(AOM_save(spec_dataset_rev));
								//ITKCALL(AOM_unlock(spec_dataset_rev));
							}
						}

						break;
					}
				}

				MEM_free(secondary_objects);
			}
		}
	}
}
/**
* @}
*/