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