You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

510 lines
14 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "stdafx.h"
#include "ae/dataset.h"
#include "sa/sa.h"
#include <tc\preferences.h>
#include <ae/datasettype.h>
#include <sa/tcfile.h>
#include<assert.h>
#include<stdio.h>
#include<string.h>
#include <vector>
#include <res/reservation.h>
#include <form/form.h>
#include <vector>
#include "stdafx.h"
#include "ae/dataset.h"
#include "sa/sa.h"
#include <tc\preferences.h>
#include <ae/datasettype.h>
#include <sa/tcfile.h>
#include<assert.h>
#include<stdio.h>
#include<string.h>
#include <vector>
#include <res/reservation.h>
#include <form/form.h>
#include <sa/tcfile.h>
#include "epm_handler_common.h"
#include "error_handling.h"
#include "string_utils.h"
#include <string>
#include <vector>
#include <map>
#include <direct.h>
#include <fstream>
#include <io.h>
#include <epm/epm.h>
#include <epm/epm_toolkit_tc_utils.h>
#include <ict/ict_userservice.h>
#include <tccore/item.h>
#include <ae/ae.h>
#include <tc/folder.h>
#include <tccore/aom.h>
#include <sa/sa.h>
#include <tccore/aom_prop.h>
#include <property/prop_errors.h>
#include <tccore/workspaceobject.h>
#include <tc/preferences.h>
#include <tccore/imantype.h>
#include <tccore//grm.h>
#include <tccore/grmtype.h>
#include <sa/am.h>
#include <cfm/cfm.h>
#include <bom/bom.h>
#include <tccore/uom.h>
#include <ps/ps.h>
#include <epm/signoff.h>
#include <fclasses/tc_date.h>
#include <ics\ics2.h>
#include <stdio.h>
#include <io.h> // 在Windows系统中需要用到这个头文件
#include <stdlib.h> // 提供exit和 EXIT_FAILURE 宏
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <itk\bmf.h>
#include <bmf\libuserext_exports.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <stdio.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define ITK_err 919821
extern "C" int POM_AM__set_application_bypass(logical bypass);
string replace_all_distinct(char* strC, char* old_valueC, char* new_valueC)
{
string str = strC;
string old_value = old_valueC;
string new_value = new_valueC;
for (string::size_type pos(0); pos != string::npos; pos += new_value.length()) {
if ((pos = str.find(old_value, pos)) != string::npos)
str.replace(pos, old_value.length(), new_value);
else break;
}
return str;
//strC = bstr;
//strcpy(strC,bstr);
}
void Split2(string strArg, string spliter, vector<string>& ans)
{
ans.clear();
size_t index0 = 0;
string one_arg;
if (strArg.find_first_not_of(' ') == string::npos)
strArg = "";
while (strArg.size() > 0)
{
index0 = strArg.find_first_of(spliter);
if (index0 != string::npos)
{
one_arg = strArg.substr(0, index0);
strArg = strArg.substr(index0 + 1);
ans.push_back(one_arg);
}
else
{
ans.push_back(strArg);
break;
}
}
}
char* removeLastCharsFromPath(char* filePath, int n) {
char* ext = strrchr(filePath, '.');
if (ext && strlen(ext) >= 5) {
ext[strlen(ext) - n] = '\0';
}
return filePath; // 返回修改后的路径
}
int fileExists(const char* filePath) {
struct stat st;
return stat(filePath, &st) == 0;
}
bool file_exists(const char* filepath) {
struct stat st;
if (stat(filepath, &st) == 0) {
// 文件存在
return true;
} else {
// 文件不存在或出现错误
if (errno == ENOENT) {
// 明确是文件不存在
return false;
} else {
// 其他错误打印错误信息并返回false
perror("An error occurred while checking the file");
return false;
}
}
}
int PX_set_signoff(EPM_action_message_t msg) {
printf("===================================\n");
printf("word/excel文档签名 开始\n");
printf("===================================\n");
int ifail = ITK_ok;
int att_cnt = 0, jpg_count = 0;
tag_t task_tag = NULLTAG,
root_task_tag = NULLTAG,
* attachments = NULL;
//节点循环处理变量
char* argflag = NULL, * argvalue = NULL, * arg = NULL;
int arg_cnt = 0;
char sign_attr[2048] = "";
arg_cnt = TC_number_of_arguments(msg.arguments);
printf("参数个数为:%d\n", arg_cnt);
if (arg_cnt > 0)
{
for (int i = 0; i < arg_cnt; i++)
{
//获取下一个参数从0开始
arg = TC_next_argument(msg.arguments);
//获取参数的名称和值
ITK_ask_argument_named_value((const char*)arg, &argflag, &argvalue);
if (strcmp(argflag, "sign") == 0)
{
if (argvalue != NULL)
{
strcpy(sign_attr, argvalue);
}
}
}
MEM_free(argflag);
MEM_free(argvalue);
}
tag_t itemrevision = NULLTAG;
printf("开超级权限\n");
POM_AM__set_application_bypass(true);
task_tag = msg.task;
char* taskName;
EPM_ask_root_task(task_tag, &root_task_tag);
AOM_ask_value_string(root_task_tag, "job_name", &taskName);
//EPM_ask_name2(root_task_tag, &taskName);
EPM_ask_attachments(root_task_tag, EPM_target_attachment, &att_cnt, &attachments);
//首选项的值
printf("获取首选项的值\n");
int path_count; char** path_values;
PREF_ask_char_values("PX_SignatureImagePath", &path_count, &path_values);
if (path_count==0) {
EMH_store_error_s1(EMH_severity_user_error, EMH_USER_error_base, "首选项【PX_SignatureImagePath】未配置。请检查");
return -1;
}
if (att_cnt > 0) {
for (int i = 0; i < att_cnt; i++)
{
int doc_num = 0;
tag_t* doc_tags = NULL;
tag_t* dataFile = NULL;
int fileNum = 0;
char* tag_type;
char* type;
ITKCALL(AOM_ask_value_string(attachments[i], "object_type", &tag_type));
printf("tag_type=%s\n", tag_type);
if (strstr(tag_type, "Revision") != NULL && strstr(tag_type, "Master") == NULL)
{
itemrevision = attachments[i];
/* int values_count = 0;
char** values;
char* value;
ITKCALL(PREF_ask_char_values("HS2_signoff_attr", &values_count, &values));*/
vector<string> sign_vec;
Split2(sign_attr, ";", sign_vec);
char signoffValue[1024] = "\0";
vector<string> type_vec;
char* value;
for (int i = 0; i < sign_vec.size(); i++)
{
printf("values=%s\n", sign_vec[i].c_str());
Split2(sign_vec[i].c_str(), "=", type_vec);
ITKCALL(AOM_ask_value_string(itemrevision, type_vec[1].c_str(), &value));
strcat(signoffValue, type_vec[0].c_str());
strcat(signoffValue, "=");
//判断是否包含后缀名
//判断路径下是否有文件存在
char* getpath = path_values[0];
printf("getpath:%s\n",getpath);
char temPath[512];
strcpy(temPath, getpath); // 先复制str1的内容
strcat(temPath, value); // 再拼接str2的内容
printf("temPath:%s\n",temPath);
char jpgfile[600];
char pngfile[600];
char tiffile[600];
strcpy(jpgfile,temPath);
strcat(jpgfile,".jpg");
strcpy(pngfile,temPath);
strcat(pngfile,".png");
strcpy(tiffile,temPath);
strcat(tiffile,".tif");
printf("判断%s是否存在",&jpgfile);
printf("判断%s是否存在",&pngfile);
printf("判断%s是否存在",&tiffile);
if (file_exists(jpgfile)) {
printf("%s存在",&jpgfile);
strcat(signoffValue, jpgfile);
}else if(file_exists(pngfile)){
printf("%s存在",&pngfile);
strcat(signoffValue, pngfile);
}else if(file_exists(tiffile)){
printf("%s存在",&tiffile);
strcat(signoffValue, tiffile);
}else{
printf("都不存在");
strcat(signoffValue, value);
}
strcat(signoffValue, "|");
MEM_free(value);
type_vec.clear();
}
//MEM_free(values);
printf("signoffValue=%s\n", signoffValue);
char path[128] = "\0";
strcat(path, getenv("TEMP"));
strcat(path, "\\");
strcat(path, "signoff.txt");
FILE* fpWrite = fopen(path, "w");
fprintf(fpWrite, "%s", signoffValue);
fclose(fpWrite);
printf("path:%s",path);
ITKCALL(AOM_ask_value_tags(itemrevision, "IMAN_specification", &doc_num, &doc_tags));
if (doc_num > 0)
{
for (int i = 0; i < doc_num; i++)
{
ITKCALL(AOM_ask_value_string(doc_tags[i], "object_type", &type));
printf("type=%s\n", type);
tag_t spec_dataset_rev = NULLTAG, ref_object = NULLTAG;
AE_reference_type_t reference_type;
printf("获取最新的数据及版本\n");
AE_ask_dataset_latest_rev(doc_tags[i], &spec_dataset_rev);
printf("获取最新的数据及版本\n");
char* new_file_name = NULL;
char origin_file_name[1024] = "";
char pathname[SS_MAXPATHLEN] = "";
char ref_name[WSO_name_size_c + 1] = "";
char cmd[256] = "";
bool flag = false;
printf("开始判读文件类型\n");
if (strcmp(type, "MSWordX") == 0) {
//ITKCALL(AOM_ask_value_tags(doc_tags[i],"ref_list",&fileNum,&dataFile));
strcpy(cmd, "SubstMacros-MSWord.wsf");
strcpy(ref_name, "word");
AE_ask_dataset_named_ref(spec_dataset_rev, ref_name, &reference_type, &ref_object);
if (reference_type == AE_PART_OF)
{
char new_ds_name[WSO_name_size_c + 1] = "";
new_file_name = USER_new_file_name(new_ds_name, ref_name, "docx", 0);
flag = true;
}
}
else if (strcmp(type, "MSWord") == 0) {
//ITKCALL(AOM_ask_value_tags(doc_tags[i],"ref_list",&fileNum,&dataFile));
strcpy(cmd, "SubstMacros-MSWord.wsf");
strcpy(ref_name, "word");
AE_ask_dataset_named_ref(spec_dataset_rev, ref_name, &reference_type, &ref_object);
if (reference_type == AE_PART_OF)
{
char new_ds_name[WSO_name_size_c + 1] = "";
new_file_name = USER_new_file_name(new_ds_name, ref_name, "doc", 0);
flag = true;
}
}
else if (strcmp(type, "MSExcelX") == 0)
{
printf("开始调用脚本");
//ITKCALL(AOM_ask_value_tags(doc_tags[i],"ref_list",&fileNum,&dataFile));
strcpy(cmd, "SetPicture-MSExcel.wsf");
strcpy(ref_name, "excel");
AE_ask_dataset_named_ref(spec_dataset_rev, ref_name, &reference_type, &ref_object);
if (reference_type == AE_PART_OF)
{
char new_ds_name[WSO_name_size_c + 1] = "";
new_file_name = USER_new_file_name(new_ds_name, ref_name, "xlsx", 0);
flag = true;
}
}
else if (strcmp(type, "MSExcel") == 0)
{
printf("开始获取调用脚本\n");
//ITKCALL(AOM_ask_value_tags(doc_tags[i],"ref_list",&fileNum,&dataFile));
strcpy(cmd, "SetPicture-MSExcel.wsf");
strcpy(ref_name, "excel");
AE_ask_dataset_named_ref(spec_dataset_rev, ref_name, &reference_type, &ref_object);
if (reference_type == AE_PART_OF)
{
char new_ds_name[WSO_name_size_c + 1] = "";
new_file_name = USER_new_file_name(new_ds_name, ref_name, "xls", 0);
flag = true;
}
}
if (ref_object == NULLTAG || !flag) {
continue;
}
printf("1\n");
ITKCALL(IMF_ask_file_pathname(ref_object, SS_WNT_MACHINE, pathname));
IMF_ask_original_file_name(ref_object, origin_file_name);
char* temp_dir = getenv("temp");
char temp_file[2048] = "";
char temp_file1[2048] = "";
char temp_file2[2048] = "";
char time1[256] = "\0";
char file_path[128] = "\0";
printf("2\n");
time_t t = time(0);
tm* local = localtime(&t);
sprintf(time1, "%d%d%d%d%d%d", local->tm_year, local->tm_mon, local->tm_mday, local->tm_hour, local->tm_min, local->tm_sec);
strcpy(temp_file, temp_dir);
string folderPath = "";
folderPath.append("D:\\Siemens\\Officesignofflogs\\");
string task_name = taskName;
printf("3\n");
if (strstr(task_name.c_str(), "/") != NULL) {
task_name = replace_all_distinct(taskName, "/", " ");
}
if (strstr(task_name.c_str(), "\\") != NULL) {
task_name = replace_all_distinct(taskName, "\\", " ");
}
folderPath.append(task_name);
folderPath.append("-");
folderPath.append(time1);
if (0 != _access(folderPath.c_str(), 0))
{
// if this folder not exist, create a new one.
ITKCALL(::_mkdir(folderPath.c_str())); // 返回 0 表示创建成功,-1 表示失败
//换成 ::_mkdir ::_access 也行,不知道什么意思
}
printf("4\n");
strcat(temp_file, "\\");
printf("4.1\n");
printf("4.2\n");
printf( "临时文件路径:%s", temp_file);
strcat(temp_file, new_file_name);
printf("temfile:%s",temp_file);
if ((_access(temp_file, 0)) != -1) {
if (!remove(temp_file)) {
remove(temp_file);
}
}
printf("\n开始导出文件\n");
ITKCALL(IMF_export_file(ref_object, temp_file));
char* filName;
ITKCALL(AOM_ask_value_string(ref_object,"file_name",&filName));
printf("4.3\n");
strcpy(temp_file1, folderPath.c_str());
printf("4.4\n");
strcat(temp_file1, "\\");
printf("4.5\n");
strcat(temp_file1, "签字前文件 ");
printf("4.6\n");
strcat(temp_file1, new_file_name);
printf("4.7\n");
strcpy(temp_file2, folderPath.c_str());
printf("4.8\n");
strcat(temp_file2, "\\");
printf("4.9\n");
strcat(temp_file2, "签字后文件 ");
printf("4.10\n");
strcat(temp_file2, new_file_name);
printf("4.11\n");
printf("5\n");
string from = temp_file, to = temp_file1, to2 = temp_file2;
ifstream in(from, ios::in | ios::binary);
if (!in)
{
return 1;
}
ofstream out(to, ios::out | ios::binary);
char ch;
while (in.get(ch))
{
out.put(ch);
}
printf("6\n");
in.close();
out.close();
strcat(cmd, " \"");
strcat(cmd, temp_file);
strcat(cmd, "\" \"");
strcat(cmd, path);
strcat(cmd, "\"");
printf("\n%s\n", cmd);
system(cmd);
char ch2;
ifstream in2(from, ios::in | ios::binary);
if (!in2)
{
return 1;
}
ofstream out2(to2, ios::out | ios::binary);
//char ch;
while (in2.get(ch2))
{
out2.put(ch2);
}
in.close();
out.close();
tag_t new_file_tag = NULLTAG;
IMF_file_t file_descriptor;
IMF_import_file(temp_file, new_file_name, SS_BINARY, &new_file_tag, &file_descriptor);
IMF_set_original_file_name(new_file_tag, origin_file_name);
IMF_close_file(file_descriptor);
AOM_save(new_file_tag);
//AOM_unlock(new_file_tag);
AOM_lock(spec_dataset_rev);
AE_remove_dataset_named_ref_by_tag(spec_dataset_rev, ref_name, ref_object);
AE_add_dataset_named_ref(spec_dataset_rev, ref_name, AE_PART_OF, new_file_tag);
AOM_save(spec_dataset_rev);
AOM_unlock(spec_dataset_rev);
}
}
}
}
}
printf("关超级权限\n");
POM_AM__set_application_bypass(false);
printf("===================================\n");
printf("word/excel文档签名 结束\n");
printf("===================================\n");
return ifail;
}