RequestNewOp.cpp
3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "RequestNewOp.h"
#include "../DataRetriever/DataRetriever.h"
using namespace TREPS::DataRetriever;
namespace TREPS
{
namespace RequestManager
{
RequestNewOpClass::RequestNewOpClass(void):RequestAbstractClass() , fileRetrieved(false), isTestDir(false), type("") , location(""), errorMsg("")
{
}
string RequestNewOpClass::getRequestId(void)
{
return "new_op";
}
bool RequestNewOpClass::load(RequestLoaderClass *loader)
{
this->errorMsg = "";
this->fileRetrieved = false;
//use test dir for the new operation?
string useTestDir = loader->getArgStrByName("testdir");
this->isTestDir = (useTestDir.compare("true") == 0);
//get new op type
// * local = from source local file path
// * url = from Web URL
this->type = loader->getArgStrByName("type");
if (this->type.compare("") == 0)
{
this->errorMsg = "Cannot load newop type";
LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
return false;
}
//source local file path or Web URL
this->location = loader->getArgStrByName("location");
if (this->location.compare("") == 0)
{
this->errorMsg = "Cannot load newop location";
LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
return false;
}
return true;
}
bool RequestNewOpClass::run(void)
{
this->errorMsg = "";
this->fileRetrieved = false;
LOG4CXX_INFO(this->app->getLog()->getPtr(),"Run request " << this->getRequestId());
//create a new working directory (ie = a new operation)
string dirId = this->dirMgr->createNewDir(this->isTestDir);
if (dirId.compare("") == 0)
{
this->errorMsg = "Cannot create new working directory";
LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
return false;
}
LOG4CXX_INFO(this->app->getLog()->getPtr(),"New operation has been created : " << dirId);
this->opId = dirId;
//lock dir
this->dirMgr->lockDir(this->opId.c_str());
//copy source file in working directory.
//Note: source file name in working directory = data.src
DataRetrieverClass *dataRtv = new DataRetrieverClass();
string dirPath = this->dirMgr->getWorkingDirFromId(this->opId.c_str());
if (type.compare("local") == 0)
{
this->fileRetrieved = dataRtv->copyLocalFile(location.c_str(),dirPath.c_str(),"data.src",true);
if (!this->fileRetrieved)
this->errorMsg = dataRtv->getErrorMsg();
}
else if (type.compare("url") == 0)
{
this->fileRetrieved = dataRtv->copyWebFile(location.c_str(),dirPath.c_str(),"data.src",true);
if (!this->fileRetrieved)
this->errorMsg = dataRtv->getErrorMsg();
}
else
{
this->errorMsg = "Unknown resource type ";
this->errorMsg += type;
LOG4CXX_ERROR(this->app->getLog()->getPtr(),this->errorMsg);
}
delete dataRtv;
return this->fileRetrieved;
}
void RequestNewOpClass::writeResult(ResultWriterClass *writer)
{
if (this->opId.compare("") == 0)
{
if (this->errorMsg.compare("") == 0)
writer->setError("Cannot create a new operation");
else
writer->setError(this->errorMsg.c_str());
return;
}
if (!this->fileRetrieved)
{
if (this->errorMsg.compare("") == 0)
writer->setError("Cannot retrieve data file");
else
writer->setError(this->errorMsg.c_str());
return;
}
writer->setSuccess();
//only return the new op id
writer->addStrArg("id",this->opId.c_str());
}
string RequestNewOpClass::getResultFileSuffix(void)
{
return "";
}
string RequestNewOpClass::getXMLFilePath(void)
{
return "";
}
string RequestNewOpClass::getStringResult(void)
{
return "";
}
}
}