
#include"hz_custom.h"

/**
*
*	项目归档
*
*/
int hz_auto_revison_to_folder(EPM_action_message_t msg)
{
	POM_AM__set_application_bypass(true);
	int ifail = ITK_ok,att_cnt =0 ,i = 0;
	tag_t task_tag = NULL_TAG,rootTask_tag=NULL_TAG,type_tag =NULL_TAG,proj_folder_tag= NULL_TAG;
	tag_t * attachments = NULL;
	char type_class[TCTYPE_class_name_size_c+1] = "\0";
	vector<string> pref_vec;
	
	getPrefStrings("Connor_ProjectFolderUidOption",TC_preference_site, pref_vec);
	if(pref_vec.size()>0){
		ITK__convert_uid_to_tag(pref_vec[0].c_str(),&proj_folder_tag);
		
	}
	if(proj_folder_tag ==NULL){
		ifail = 2;
	}
	task_tag = msg.task;
	if(task_tag == NULLTAG)
	{
		POM_AM__set_application_bypass(false);
		return ITK_ok;
	}
	EPM_ask_root_task(task_tag, &rootTask_tag);
	EPM_ask_attachments(rootTask_tag, EPM_target_attachment, &att_cnt, &attachments);
	for( i = 0; i < att_cnt; i++ )
	{
		TCTYPE_ask_object_type(attachments[i], &type_tag);
		TCTYPE_ask_class_name(type_tag, type_class);
		ECHO("type_class : %s\n",type_class);	
		//过滤掉非版本对象
		if (((strstr(type_class,"Revision") != NULL) || (strstr(type_class,"revision") != NULL))
			&&(strstr(type_class,"Master") == NULL) &&(strstr(type_class,"master") == NULL)
			&& (strstr(type_class,"BOM") ==NULL) && (strstr(type_class,"bom") ==NULL) && (strstr(type_class,"Bom") == NULL))
		{
			char * h9_DocProject =NULL;
			char * h9_DocLevel2 = NULL;
			char * h9_DocLevel3 = NULL;
			char * name = NULL;
			int mycount = 0,mycount2 = 0,mycount3 = 0,j =0,k = 0,w = 0;
			tag_t *folder_tags = NULL,*folder_tags2 = NULL,*folder_tags3 = NULL;
			AOM_UIF_ask_value(attachments[i],"h9_DocProject",&h9_DocProject);
			AOM_UIF_ask_value(attachments[i],"h9_DocLevel2",&h9_DocLevel2);
			AOM_UIF_ask_value(attachments[i],"h9_DocLevel3",&h9_DocLevel3);
			AOM_ask_value_tags(proj_folder_tag,"contents",&mycount,&folder_tags);

			for(j = 0;j< mycount;j++){
				AOM_ask_value_string(folder_tags[j],"object_name",&name);
				if(strcmp(h9_DocProject,name)==0){
					if(name!=NULL){
						MEM_free(name);
						name = NULL;
					}
					AOM_ask_value_tags(folder_tags[j],"contents",&mycount2,&folder_tags2);
					for(k = 0;k < mycount2;k++){
						AOM_ask_value_string(folder_tags2[k],"object_name",&name);
						if(strcmp(name,h9_DocLevel2)==0){
							if(name!=NULL){
								MEM_free(name);
								name = NULL;
							}
							AOM_ask_value_tags(folder_tags2[k],"contents",&mycount3,&folder_tags3);
							for(w = 0 ;w < mycount3;w++){
								AOM_ask_value_string(folder_tags3[w],"object_name",&name);
								if(strcmp(name,h9_DocLevel3)==0){
									FL_insert(folder_tags3[w],attachments[i],999);
									//保存
									AOM_save(folder_tags3[w]);
									break;
								}

								if(name!=NULL){
									MEM_free(name);
									name = NULL;
								}
								
							}
						
							break;
						}

						if(name!=NULL){
							MEM_free(name);
							name = NULL;
						}
					}
					if(folder_tags2!=NULL){
						MEM_free(folder_tags2);
						folder_tags2 =NULL;
					}
					break;
				}
				if(name!=NULL){
					MEM_free(name);
					name = NULL;
				}
			
			}
			
			if(h9_DocProject!=NULL){
				MEM_free(h9_DocProject);
				h9_DocProject =NULL;
			}
			if(h9_DocLevel2!=NULL){
				MEM_free(h9_DocLevel2);
				h9_DocLevel2 =NULL;
			}
			if(h9_DocLevel3!=NULL){
				MEM_free(h9_DocLevel3);
				h9_DocLevel3 =NULL;
			}
			if(folder_tags!=NULL){
				MEM_free(folder_tags);
				folder_tags =NULL;
			}
			
			if(folder_tags3!=NULL){
				MEM_free(folder_tags3);
				folder_tags3 =NULL;
			}

		}
	}

	if(attachments!=NULL){
		MEM_free(attachments);
		attachments =NULL;
	}
	if(ifail == ITK_ok){
		EMH_clear_errors();
	}
	POM_AM__set_application_bypass(false);
	return 0;
}
/**
*
*
*/
int hz_status_check(EPM_rule_message_t msg)
{
	EPM_decision_t decision = EPM_go;
	int ifail = ITK_ok,att_cnt =0 ,i = 0,j = 0,arg_cnt = 0;
	tag_t task_tag = NULL_TAG,rootTask_tag=NULL_TAG,type_tag =NULL_TAG,proj_folder_tag= NULL_TAG,relation_type;
	tag_t * attachments = NULL;
	char type_class[TCTYPE_class_name_size_c+1] = "\0";
	char * argflag = NULL,*argvalue =NULL,*arg = NULL;
	char arg1value[1024] = "",arg2value[1024] = "",arg3value[1024]="",arg4value[1024]="";

	vector<string> include_type_v;
	vector<string> secondary_type_v;
	vector<string> allowed_status_v;

	map<string,int> errMap;
	map<string,int>::iterator err_it;

	arg_cnt = TC_number_of_arguments(msg.arguments);
	ECHO("参数个数为：%d\n",arg_cnt);
	if (arg_cnt > 0)
	{
		for (i=0;i<arg_cnt;i++)
		{
			//获取下一个参数（从0开始）
			arg = TC_next_argument(msg.arguments);
			//获取参数的名称和值
			ifail = ITK_ask_argument_named_value((const char*)arg, &argflag, &argvalue);
			//检查对象类型
			if (stricmp(argflag, "include_type") == 0)
			{
				if(argvalue != NULL)
				{
					strcpy(arg1value,argvalue);
				}else{
				
					string key_id;
					key_id.assign("缺少include_type参数");
					errMap.insert(pair<string,int>(key_id,HANDLER_ARGUMENT_ERROR));
				}
			}
			//检查次附件对象类型
			else if(stricmp(argflag, "secondary_type") == 0)
			{
				if(argvalue != NULL)
				{
					strcpy(arg2value,argvalue);
				}
				else{
				
					string key_id;
					key_id.assign("缺少secondary_type参数");
					errMap.insert(pair<string,int>(key_id,HANDLER_ARGUMENT_ERROR));
				}
			}
			//伪文件夹的名称
			else if(stricmp(argflag,"relation") == 0)
			{
				if(argvalue != NULL)
				{
					strcpy(arg3value,argvalue);
				}else{
				
					string key_id;
					key_id.assign("缺少relation参数");
					errMap.insert(pair<string,int>(key_id,HANDLER_ARGUMENT_ERROR));
				}
			}
			//检查满足条件的状态
			else if(stricmp(argflag,"allowed_status") == 0)
			{
				if(argvalue != NULL)
				{
					strcpy(arg4value,argvalue);
				}else{
				
					string key_id;
					key_id.assign("缺少allowed_status参数");
					errMap.insert(pair<string,int>(key_id,HANDLER_ARGUMENT_ERROR));
				}
			}
		}
		MEM_free(argflag);
		MEM_free(argvalue);
	}

	if(errMap.size() !=0){
		for( err_it = errMap.begin(); err_it != errMap.end(); err_it++ )
		{
			ECHO("DEBUG:  err_it is %s\n", err_it->first.c_str());
			EMH_store_error_s1(EMH_severity_information,err_it->second, err_it->first.c_str());
		}
		errMap.clear();
		return EPM_nogo;
	}

	ECHO("include_type : %s\n",arg1value);
	ECHO("secondary_type : %s\n",arg2value);
	ECHO("relation : %s\n",arg3value);
	ECHO("allowed_status : %s \n",arg4value);


	Split(arg1value,",",include_type_v);
	Split(arg2value,",",secondary_type_v);
	Split(arg4value,",",allowed_status_v);

	task_tag = msg.task;
	if(task_tag == NULLTAG)
	{
		
		return EPM_nogo;
	}
	EPM_ask_root_task(task_tag, &rootTask_tag);
	EPM_ask_attachments(rootTask_tag, EPM_target_attachment, &att_cnt, &attachments);
	for( i = 0; i < att_cnt; i++ )
	{
		TCTYPE_ask_object_type(attachments[i], &type_tag);
		TCTYPE_ask_class_name(type_tag, type_class);
		ECHO("include_type : %s\n",type_class);
		//判断类型是否属于配置的类型
		if(isInVec(type_class,include_type_v)){
			GRM_find_relation_type(arg3value, &relation_type);
			int second_tags_count = 0 ;
			tag_t * second_tags =NULL;
			GRM_list_secondary_objects_only(attachments[i],relation_type,&second_tags_count,&second_tags);
			
			for(j = 0;j < second_tags_count;j++){
				TCTYPE_ask_object_type(second_tags[j], &type_tag);
				TCTYPE_ask_class_name(type_tag, type_class);
				ECHO("secondary_type : %s\n",type_class);
				//判断是否属于子类型
				if(isInVec(type_class,secondary_type_v)){
					int  status_count = 0;
					tag_t * status_tag_list = NULL;;
					WSOM_ask_release_status_list(second_tags[j],&status_count,&status_tag_list);
					char status_type[WSO_name_size_c+1] = "";

					char * object_string =NULL;
					AOM_ask_value_string(second_tags[j],"object_string",&object_string);

					if(status_count == 0){
						string errMsg;
						errMsg.assign(object_string).append("不满足发布状态!");
						errMap.insert(pair<string,int>(errMsg,HANDLER_ARGUMENT_ERROR));
						decision = EPM_nogo;
					}

					for(int w = 0; w < status_count ;w++){
						ITKCALL(CR_ask_release_status_type(status_tag_list[w],status_type));
						
						if(!isInVec(status_type,allowed_status_v)){
							//char * object_string =NULL;
							//AOM_ask_value_string(second_tags[j],"object_string",&object_string);
							string errMsg;
							errMsg.assign(object_string).append("不满足发布状态!");
							errMap.insert(pair<string,int>(errMsg,HANDLER_ARGUMENT_ERROR));
							decision = EPM_nogo;
							//if(object_string!=NULL){
							//	MEM_free(object_string);
							//	object_string =NULL;
							//}
						}
						
					}
					if(object_string!=NULL){
						MEM_free(object_string);
						object_string =NULL;
					}
					if(status_tag_list!=NULL){
						MEM_free(status_tag_list);
						status_tag_list =NULL;
					}
				}
			}

			if(second_tags!=NULL){
				MEM_free(second_tags);
				second_tags = NULL;
			}
		}
	}
	
	for( err_it = errMap.begin(); err_it != errMap.end(); err_it++ )
	{
		ECHO("DEBUG:  err_it is %s\n", err_it->first.c_str());
		EMH_store_error_s1(EMH_severity_information,err_it->second, err_it->first.c_str());
	}
	errMap.clear();

	return decision;
}


logical isInVec(char *type, vector<string> &type_vec)
{
	int i = 0;
	logical isInclude = false;
	for( i = 0; i < type_vec.size(); i++ )
	{
		if( type_vec[i].compare(type) ==0 )
		{
			isInclude = true;
			break;
		}
	}
	return isInclude;
}