Salome HOME
Checked-out files path was changed to /tmp/SimanSalome/StudyId/ScenarioId/UserId
[tools/simanio.git] / src / SimanIO_Link.cxx
1 #include "CreateConfigFile.h"
2 #include "CreateConfigFileResponse.h"
3 #include "GetFile.h"
4 #include "GetFileResponse.h"
5 #include "PutFile.h"
6 #include "PutFileResponse.h"
7 #include "SimanSalomeServiceStub.h"
8
9 #include <Environment.h>
10 #include <iostream>
11 #include <fstream>
12 #include <sstream>
13 #include <ServiceClient.h>
14 #include <SimanIO_Link.hxx>
15
16
17 #include <algorithm>
18
19
20 using namespace std;
21 using namespace org_splat_ws_server_service_salome;
22 using namespace wso2wsf;
23
24 SimanIO_Link::SimanIO_Link(const char* theStudyID, 
25     const char* theScenarioID, const char* theUserID)
26 : myStudy(theStudyID), myScenario(theScenarioID), myUser(theUserID)
27 {
28 }
29
30 bool SimanIO_Link::IsConnected()
31 {
32   // TO DO
33   return true;
34 }
35
36 // unification of the error handling in the RetrieveConf and StoreConf  methods
37 #define CONFIGURSION_ERROR(message) {cerr<<message<<endl;}
38
39 SimanIO_Configuration SimanIO_Link::RetrieveConf()
40 {
41   SimanIO_Configuration aResult;
42   // for now use simple initial cfg file for testings purposes only
43   // in the future this file must be downloaded from the SIMAN server somehow
44   //const char* aCFGFile = "/dn20/salome/mpv/siman/text1.conf";
45   
46   char* aSimanHost = getenv("SIMAN_WS_HOST");
47   if (aSimanHost == NULL)
48   {
49     CONFIGURSION_ERROR("The SIMAN_WS_HOST variable does not exist. Please set it.\n");
50     return aResult;
51   }
52   
53   string aSimanWSHost(aSimanHost);
54   string anEndpointUri = "http://" + aSimanWSHost + "/Siman-WS/services/SimanSalomeService";
55   
56   Environment::initialize("SimanSalomeService.log",AXIS2_LOG_LEVEL_DEBUG);
57   char* aClientHomeEnv = getenv("WSFCPP_HOME");
58   
59   if (aClientHomeEnv == NULL)
60   {
61     CONFIGURSION_ERROR("The WSFCPP_HOME variable does not exist. Please set it.\n");
62     return aResult;
63   }
64   
65   string aClientHome = AXIS2_GETENV("WSFCPP_HOME");
66   SimanSalomeServiceStub* aStub = new SimanSalomeServiceStub(aClientHome, anEndpointUri);
67   ServiceClient *client = aStub->getServiceClient();
68   
69   //Set the ServiceClient options
70   client->engageModule(AXIS2_MODULE_ADDRESSING);
71   Options *options = client->getOptions();
72   axis2_options_t *axis2options = options->getAxis2Options();
73   axis2_options_set_action(axis2options, Environment::getEnv(), "urn:createConfigFile");
74   
75   //MTOM options
76   options->setTimeout(1000000);
77   options->setSoapVersion(AXIOM_SOAP12);
78   
79   //CreateConfigFile operation
80   CreateConfigFile* aCreateConfigFileRequest = new CreateConfigFile();
81   
82   aCreateConfigFileRequest->setArgs0(atoll(myStudy.c_str()));
83   aCreateConfigFileRequest->setArgs1(atoll(myScenario.c_str()));
84   aCreateConfigFileRequest->setArgs2(atoll(myUser.c_str()));
85   
86   //cout<<"StudyId = " << atoll(myStudy.c_str()) << "\n";
87   //cout<<"ScenarioId = " << atoll(myScenario.c_str()) << "\n";
88   //cout<<"UserId = " << atoll(myUser.c_str()) << "\n";
89   
90   CreateConfigFileResponse* aCreateConfigFileResponse = new CreateConfigFileResponse();
91   aCreateConfigFileResponse = aStub->createConfigFile(aCreateConfigFileRequest);
92   
93   string aResponseFilePath = aCreateConfigFileResponse->get_return();
94   
95   //cout << "Response file path = " << aResponseFilePath << "\n";
96   
97   string aResponseFileName = aResponseFilePath.substr(aResponseFilePath.find_last_of("/\\")+1);
98   
99   //Get the created config file to client
100   
101   //Set the options
102   axis2_options_set_action(axis2options, Environment::getEnv(), "urn:getFile");
103   
104   GetFile* aGetFileRequest = new GetFile();
105   aGetFileRequest->setArgs0(aResponseFilePath);
106   GetFileResponse* aGetFileResponse = new GetFileResponse();
107   aGetFileResponse = aStub->getFile(aGetFileRequest);
108   
109   //Parse the response and get the filename
110   string aTmpDir = "/tmp";
111   string aClientFileDir = aTmpDir + "/SimanSalome/" + myStudy + "/" + myScenario + "/" + myUser + "/";
112   //string aClientFileDir = aTmpDir + "/SimanSalome/" +  myUser + "/download/";
113   string aClientFilePath = aClientFileDir + aResponseFileName;
114   
115   //Cretae the directories
116   system(("mkdir -p " + aClientFileDir).c_str());
117   
118   //Download the siman-salome.conf file
119   ofstream outputStream;
120   outputStream.open(aClientFilePath.c_str(), ofstream::binary);
121   int aBufferSize = axutil_base64_binary_get_decoded_binary_len(aGetFileResponse->get_return(), Environment::getEnv());
122   int* aBufSize = new int[aBufferSize];
123   unsigned char* aCopyBuffer = axutil_base64_binary_get_plain_binary(aGetFileResponse->get_return(), Environment::getEnv(), aBufSize);
124   outputStream.write(reinterpret_cast<const char*>(aCopyBuffer),aBufferSize);
125   outputStream.flush();
126   outputStream.close();
127   delete[] aCopyBuffer;
128   
129   //Parse the downloaded siman-salome.conf file
130   aResult.Parse(aClientFilePath.c_str());
131   
132   //Set the actual file URLs and get these files onto client.
133   SimanIO_Configuration::ActivitiesIterator actIter(aResult);
134   for(; actIter.More(); actIter.Next()) {
135     SimanIO_Activity::DocumentsIterator aDocIter(actIter.Activity());
136     for(; aDocIter.More(); aDocIter.Next()) {
137       /*const*/ SimanIO_Document& aDoc = aDocIter.Document();
138       SimanIO_Document::FilesIterator aFileIter(aDoc);
139       for(; aFileIter.More(); aFileIter.Next()) {
140         string aURL = aFileIter.URL();
141         string aFileName = aURL.substr(aURL.find_last_of("/\\")+1);
142         int startUnderlineIndex = aFileName.find_first_of("_") + 1;
143         string aSourceFileName = aFileName.substr(startUnderlineIndex);
144         
145         //cout << "aURL = " << "http://" << aSimanWSHost << "/repository/" << aURL << "\n";
146         string aPathToVault = aResponseFilePath.substr(0, aResponseFilePath.find("download")) + "vault/";
147         
148         //only for test only for WIN
149         //replace(aURL.begin(),aURL.end(),'/','\\');
150         //end only for test only for WIN
151         
152         //cout << "Path to vault" << aPathToVault + aURL << "\n";
153         
154         //Get the files
155         //aGetFileRequest->setArgs0("http://" + aSimanWSHost + "/repository/" + aURL);
156         aGetFileRequest->setArgs0(aPathToVault + aURL);
157         aGetFileResponse = aStub->getFile(aGetFileRequest);
158         
159         outputStream.open((aClientFileDir + aSourceFileName).c_str(), ofstream::binary);
160         int aBufferSize = axutil_base64_binary_get_decoded_binary_len(aGetFileResponse->get_return(), Environment::getEnv());
161         int* aBufSize = new int[aBufferSize];
162         unsigned char* aCopyBuffer = axutil_base64_binary_get_plain_binary(aGetFileResponse->get_return(), Environment::getEnv(), aBufSize);
163         outputStream.write(reinterpret_cast<const char*>(aCopyBuffer),aBufferSize);
164         outputStream.flush();
165         outputStream.close();
166         delete[] aCopyBuffer;
167
168         //Set new URLs
169         aFileIter.SetURL((aClientFileDir + aFileName).c_str());
170         //cout << "New location of the files " << aClientFileDir + aFileName << "\n";
171       }//aFileIter
172     }//aDocIter
173   }//actIter
174
175
176   //Delete objects
177   delete aCreateConfigFileRequest;
178   delete aCreateConfigFileResponse;
179   delete aGetFileResponse;
180   delete aGetFileRequest;
181   delete aStub;
182   
183   return aResult;
184 }
185
186 void SimanIO_Link::StoreConf(/*const*/ SimanIO_Configuration& theConf)
187 {
188   cout << "StoreConf method is started\n";
189   char* aSimanHost = getenv("SIMAN_WS_HOST");
190   if (aSimanHost == NULL)
191   {
192     CONFIGURSION_ERROR("The SIMAN_WS_HOST variable does not exist. Please set it.\n");
193   }
194   
195   string aSimanWSHost(aSimanHost);
196   string anEndpointUri = "http://" + aSimanWSHost + "/Siman-WS/services/SimanSalomeService";
197   
198   Environment::initialize("SimanSalomeService.log",AXIS2_LOG_LEVEL_DEBUG);
199   char* aClientHomeEnv = getenv("WSFCPP_HOME");
200   
201   if (aClientHomeEnv == NULL)
202   {
203     CONFIGURSION_ERROR("The WSFCPP_HOME variable does not exist. Please set it.\n");
204   }
205   
206   string aClientHome = AXIS2_GETENV("WSFCPP_HOME");
207   SimanSalomeServiceStub* aStub = new SimanSalomeServiceStub(aClientHome, anEndpointUri);
208   ServiceClient *client = aStub->getServiceClient();
209   
210   //Set the ServiceClient options
211   client->engageModule(AXIS2_MODULE_ADDRESSING);
212   Options *options = client->getOptions();
213   axis2_options_t *axis2options = options->getAxis2Options();
214   axis2_options_set_action(axis2options, Environment::getEnv(), "urn:putFile");
215   
216   //MTOM options
217   options->setTimeout(1000000);
218   options->setSoapVersion(AXIOM_SOAP12);
219   
220   //Upload the file onto server
221   PutFile* aPutFileRequest = new PutFile();
222   PutFileResponse* aPutFileResponse = new PutFileResponse();
223   
224   //Prepare salome-siman.conf file
225   string aTmpDir = "/tmp";
226   string aClientFileDir = aTmpDir + "/SimanSalome/" + myStudy + "/" + myScenario + "/" + myUser + "/";
227   //string aClientFileDir = aTmpDir + "/SimanSalome/" +  myUser + "/download/";
228   string aClientFilePath = aClientFileDir + "salome-siman.conf";
229   ofstream aSalomeSimanFile;
230   aSalomeSimanFile.open(aClientFilePath.c_str());
231   
232   //Get the actual file URLs and put these files onto server.
233   SimanIO_Configuration::ActivitiesIterator actIter(theConf);
234
235   for(; actIter.More(); actIter.Next()) {
236     SimanIO_Activity::DocumentsIterator aDocIter(actIter.Activity());
237     for(; aDocIter.More(); aDocIter.Next()) {
238       /*const*/ SimanIO_Document& aDoc = aDocIter.Document();
239       SimanIO_Document::FilesIterator aFileIter(aDoc);
240       for(; aFileIter.More(); aFileIter.Next()) {
241         string aURL = aFileIter.URL();
242         string aFileName = aURL.substr(aURL.find_last_of("/\\")+1);
243         cout << "aURL = " << aURL << "\n";
244         cout << " aFileName= " << aFileName << "\n";
245         //get size of the input file
246         ifstream inputStream (aURL.c_str(), ifstream::binary);
247         inputStream.seekg(0,ifstream::end);
248         int aSize = inputStream.tellg();
249         inputStream.seekg(0);
250         
251         //Upload action
252         char* aCopyBuffer;
253         aCopyBuffer = new char [aSize];
254         inputStream.read(aCopyBuffer,aSize);
255         axutil_base64_binary_t* aRequestDH = axutil_base64_binary_create_with_plain_binary(Environment::getEnv(), reinterpret_cast<unsigned char*>(aCopyBuffer), aSize);
256         inputStream.close();
257         delete[] aCopyBuffer;
258
259         //Set arguments
260         aPutFileRequest->setArgs0(aRequestDH);
261         aPutFileRequest->setArgs1(aFileName);
262         aPutFileRequest->setArgs2(atoll(myUser.c_str()));
263         
264         aPutFileResponse = aStub->putFile(aPutFileRequest);
265         
266         //Write information into salome-siman.conf file
267         ostringstream activityId;
268         ostringstream documentId;
269         activityId << actIter.ActivityId();
270         documentId << aDocIter.DocId();
271         aSalomeSimanFile << myScenario +  "," +  myUser + "," + activityId.str() +  "," + documentId.str() + "," + aPutFileResponse->get_return() + "\n";
272
273       }//aFileIter
274     }//aDocIter
275   }//actIter
276
277   aSalomeSimanFile.close();
278   
279   //Put the salome-siman.conf file onto server
280   ifstream inputStream (aClientFilePath.c_str(), ifstream::binary);
281   inputStream.seekg(0,ifstream::end);
282   int aSize = inputStream.tellg();
283   inputStream.seekg(0);
284   
285   char* aCopyBuffer;
286   aCopyBuffer = new char [aSize];
287   inputStream.read(aCopyBuffer,aSize);
288   axutil_base64_binary_t* aRequestDH = axutil_base64_binary_create_with_plain_binary(Environment::getEnv(), reinterpret_cast<unsigned char*>(aCopyBuffer), aSize);
289   inputStream.close();
290   delete[] aCopyBuffer;
291
292   //Set arguments
293   aPutFileRequest->setArgs0(aRequestDH);
294   aPutFileRequest->setArgs1("salome-siman.conf");
295   aPutFileRequest->setArgs2(atoll(myUser.c_str()));
296
297   aPutFileResponse = aStub->putFile(aPutFileRequest);
298
299   //Check in operation
300   axis2_options_set_action(axis2options, Environment::getEnv(), "urn:checkIn");
301   
302   CheckIn* aCheckInRequest = new CheckIn();
303   CheckInResponse* aCheckInResponse = new CheckInResponse();
304   aCheckInRequest->setArgs0(aPutFileResponse->get_return());
305   aCheckInRequest->setArgs1(atoll(myUser.c_str()));
306   aCheckInResponse = aStub->checkIn(aCheckInRequest);
307   
308   //Delete the directories
309   system(("rm -r " + aTmpDir + "/SimanSalome/").c_str());
310   
311   //Delete objects
312   delete aPutFileRequest;
313   delete aPutFileResponse;
314   delete aCheckInRequest;
315   delete aCheckInResponse;
316   delete aStub;
317 }