/** * @addtogroup service * @{ */ /** * @file service_part_no.cpp * @brief retrieve new item id according some mark code * @date 2009/7/26 * @author James Pang * @history * =================================================================================== * Date Name Description * 26-Jul-2009 James created */ #pragma warning(disable:4819) #pragma warning(disable:4267) /** * @headerfile tcua 头文件 */ #include #include #include #include #include #include #include #include #include #include /** * @headerfile standard c & cpp header files */ #include #include #include #include #include #include /** * @headerfile user's header files */ #include "itk_service_common.h" #include "error_handling.h" #define CONSOLE_PRINT 1 /* Define the number of retries when fetching the next ID. */ /* This must be stored in both numeric and string forms. */ #define GET_ID_RETRIES 10 #define CYCLE_TIMES 10 #define STARTNUMBER 1 #define PADNUMBER 4 #define RETRY_STRING "10" #define HANDLE_ERROR_S1(E,S) \ { EMH_store_initial_error_s1(EMH_severity_error,(E),(S)); return(E); } #define REPORT_IFAIL(M) \ ifail?printf("%s : ERROR : ifail = %d\n",M,ifail):printf("%s : Ok\n",M) #define HANDLE_ERROR(E) \ { EMH_store_initial_error(EMH_severity_error,(E)); return(E); } #define HANDLE_ERROR_S1(E,S) \ { EMH_store_initial_error_s1(EMH_severity_error,(E),(S)); return(E); } #define HANDLE_ERROR_AND_RELEASE(E,I) \ { \ (void)release_number(I); \ EMH_store_initial_error(EMH_severity_error,(E)); \ return(E); \ } #define TEST_ERROR(E) \ if ((E) != ITK_ok) return(E); #define TEST_POM_ERROR(E) \ if ((E) != ITK_ok) HANDLE_ERROR(E) #define TEST_ERROR_AND_RELEASE(E,I) \ if ((E) != ITK_ok) { (void)release_number(I); return(E); } /* Define the number of retries when fetching the next ID. */ /* This must be stored in both numeric and string forms. */ #define GET_ID_RETRIES 10 #define RETRY_STRING "10" /* Empty string test macro */ #define IS_EMPTY(S) (!(*(S))) /* Define the methods by which a new item name can be obtained */ #define SEED_METHOD 0 #define BUILD_METHOD 1 /* The following define the maximum index number to be used. * * Note that the actual maximum is one less than MAXNUM. * * NUMERIC_LIMIT must be large enough to store decimal string * * version of MAXNUM plus a NULL terminator. */ #define NUMERIC_LIMIT 12 #define MAXNUM 0x7FFFFFFF /* define the class for which id and rev is generated */ #define CLASS_IS_ITEM 0 #define CLASS_IS_DATASET 1 /* Store the tags for the next_id POM object and the actual ID attribute. */ /* This saves us having to obtain these every time a user hits the Assign */ /* button. The logical variables define whether the tags are legal or not. */ /* I tried to set to tag_t's to NULLTAG for this purpose, but the HP C */ /* compiler did not like this. Typical. */ static logical inst_ok = false; static logical attr_ok = false; static tag_t instance; static tag_t attr_id; static logical is_new = false; /*Store the class identifier for the id and rev generation. */ /* This helps us share some of the existing codes */ static int class_is = -1; /* Store all Item IDs issued, whether they are used or not.*/ static int item_ids_issued = 0; static char** item_ids_issued_list = NULL; static char* ITEM_POM_CLASS = "ITEM"; /*------------------------------------------------------------------------------------------------------------------------------*/ /* * * int release_number ( tag_t instance ) * * Release the lock on the index number in the database. This is only * CALLed in error situations. * * Parameters: * instance The tag of the POM object to be released. This is * returned by the load function. * */ static int release_number ( tag_t instance ) { if (instance != NULLTAG) { return POM_refresh_instances_any_class(1, &instance, POM_no_lock); } else { return ITK_ok; } } /* end release_number */ static int ORIGIN_select_counter ( const char *counter_name,int num ) { int i, n; tag_t class_id; tag_t name_attr,num_attr; tag_t *inst_tag; char *value; int num_value; logical vNull, vEmpty; int ifail; instance = NULLTAG; attr_id = NULLTAG; inst_ok = false; attr_ok = false; /* Find the Next ID class */ POM_class_id_of_class("ImanNextId", &class_id); /* Find all the instances of this class */ ITKCALL(ifail = POM_instances_of_class(class_id, false, &n, &inst_tag)) /* Locate the appropriate attribute */ ITKCALL(ifail = POM_attr_id_of_attr("name","ImanNextId", &name_attr)) ITKCALL(ifail = POM_attr_id_of_attr ("next_id","ImanNextId",&num_attr)) /* Try to locate the named instance */ for (i=0; ((i50) return -1; if(TeZhengMa==NULL) return -1; strcpy(pad_string,"\0"); sprintf(str_num,"%d",curr_num); printf("str_num = %s\n",str_num); printf("strlen = %d\n",strlen(str_num)); //new_name = (char*)malloc(sizeof(char)*100); //strcpy(new_name,""); if((num_len-(int)strlen(str_num))>0) { printf("111\n"); pad_string[num_len-strlen(str_num)] = '\0'; for(i=0;inumber) { ifail = POM_set_attr_int( 1, &instance, attr_id, start_num); //*sNum = start_num; } else { ifail = POM_set_attr_int( 1, &instance, attr_id, number+i); //*sNum = number+i; } //TEST_POM_ERROR(ifail) ifail = POM_save_instances( 1, &instance, true ); TEST_POM_ERROR(ifail); printf("sNum = %s\n",*sNum); return ifail; } int GetSeqNo(char* TeZhengMa, int length, int start_num, int end_num,int step,int *sNum) { int ifail; logical tagValid = false ; int number; int *rtn; char counter_name[255]; void *returnValueType; sprintf(counter_name,"%s$%d$%d$%d$%d",TeZhengMa,length,start_num,end_num,step); printf("counter_name = %s\n",counter_name); POM_is_tag_valid( instance , &tagValid); ifail = ORIGIN_select_counter(counter_name,start_num); TEST_ERROR(ifail) ifail = load_current_number(counter_name,&number); printf("counter number = %d\n",number); if (!attr_ok) { ifail = get_next_id_attribute(); TEST_ERROR(ifail) } if(is_new) ifail = POM_set_attr_int( 1, &instance, attr_id, start_num); else { if(number+step>end_num) { *sNum=number+step; return -1; } ifail = POM_set_attr_int( 1, &instance, attr_id, number+step); } TEST_POM_ERROR(ifail) ifail = POM_save_instances( 1, &instance, true ); TEST_POM_ERROR(ifail) //(int*)returnValueType = number+step; //printf("current_number = %d\n",number+step); //printf("returnValueType = %d\n",returnValueType); if(is_new) *sNum = start_num; else *sNum = number+step; return ifail; //return number+step; //return ITK_ok; } int SERVICE_part_no( void *retValType ) { int ifail = ITK_ok, i=0; char *argTeZhengMa=NULL, strTeZhengMa[ITEM_id_size_c + 1] = "", *new_num=NULL, counter_name[256] = "", temp[ITEM_id_size_c + 1] = "", *data = NULL; int arg1 = 0, arg2 = 0, arg3 = 0, arg4 = 0, num = 0, rows = 0; //取得客户端java所传来的参数 ifail = USERARG_get_string_argument(&argTeZhengMa); ifail = USERARG_get_int_argument(&arg1); ifail = USERARG_get_int_argument(&arg2); ifail = USERARG_get_int_argument(&arg3); ifail = USERARG_get_int_argument(&arg4); if(argTeZhengMa!=NULL) { strcpy(strTeZhengMa,argTeZhengMa); } //调用自己的方法,参数来自Java ifail = GetSeqNo(strTeZhengMa,arg1,arg2,arg3,arg4,&num); TEST_ERROR(ifail) sprintf(temp,"%d",num); do { i++; ifail = generation_num(strTeZhengMa,arg1,num,&new_num); //char counter_name[256] = ""; strcpy(counter_name,"%"); strcat(counter_name,new_num); strcat(counter_name,"%"); ifail = EnqLikeNo(counter_name,&rows); if ( rows != 0 ) free(new_num); } while(rows!=0 && i<10000); if(num>arg3) strcpy(temp,"Error: Current WaterNumber is out of Max Value!"); else sprintf(temp,"%s",new_num); //data = (char *) MEM_alloc ( sizeof(char)*(strlen(pszID) + 1 ) ); printf("new_num= %s\n",new_num); data = (char *)MEM_alloc((ITEM_id_size_c + 1)*sizeof(char)); strcpy( data, temp ); *((char**) retValType) = data; MEM_free(new_num); printf( "new itemid = %s\n", *(char**)retValType); return ITK_ok; } int SERVICE_internal_part_no( const char *argMark, int length, int start_num, int end_num,int step, char **newID ) { int ifail = ITK_ok, i=0; char strTeZhengMa[ITEM_id_size_c + 1] = "", *new_num=NULL, counter_name[256] = "", temp[ITEM_id_size_c + 1] = "", *data = NULL; int arg1 = length, arg2 = start_num, arg3 = end_num, arg4 = step, num = 0, rows = 0; if(argMark!=NULL) { strcpy( strTeZhengMa, argMark ); } //调用自己的方法,参数来自Java ifail = GetSeqNo(strTeZhengMa,arg1,arg2,arg3,arg4,&num); TEST_ERROR(ifail) sprintf(temp,"%d",num); do { i++; ifail = generation_num(strTeZhengMa,arg1,num,&new_num); //char counter_name[256] = ""; strcpy(counter_name,"%"); strcat(counter_name,new_num); strcat(counter_name,"%"); ifail = EnqLikeNo(counter_name,&rows); if ( rows != 0 ) free(new_num); } while(rows!=0 && i<10000); if(num>arg3) strcpy(temp,"Error: Current WaterNumber is out of Max Value!"); else sprintf(temp,"%s",new_num); MEM_free(new_num); *newID = (char *)MEM_alloc((ITEM_id_size_c + 1)*sizeof(char)); strcpy( *newID, temp ); printf( "new itemid = %s\n", temp); return ITK_ok; } /** * @} */