]> SALOME platform Git repositories - tools/simanio.git/blob - src/SimanIO_Link.cxx
Salome HOME
Check-in operation without any modifications is possible
[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   
115   //Download the siman-salome.conf file
116   ofstream outputStream;
117   outputStream.open(aClientFilePath.c_str(), ofstream::binary);
118   int aBufferSize = axutil_base64_binary_get_decoded_binary_len(aGetFileResponse->get_return(), Environment::getEnv());
119   int* aBufSize = new int[aBufferSize];
120   unsigned char* aCopyBuffer = axutil_base64_binary_get_plain_binary(aGetFileResponse->get_return(), Environment::getEnv(), aBufSize);
121   outputStream.write(reinterpret_cast<const char*>(aCopyBuffer),aBufferSize);
122   outputStream.flush();
123   outputStream.close();
124   delete[] aCopyBuffer;
125   
126   //Parse the downloaded siman-salome.conf file
127   aResult.Parse(aClientFilePath.c_str());
128   
129   //Set the actual file URLs and get these files onto client.
130   SimanIO_Configuration::ActivitiesIterator actIter(aResult);
131   for(; actIter.More(); actIter.Next()) {
132     SimanIO_Activity::DocumentsIterator aDocIter(actIter.Activity());
133     for(; aDocIter.More(); aDocIter.Next()) {
134       /*const*/ SimanIO_Document& aDoc = aDocIter.Document();
135       SimanIO_Document::FilesIterator aFileIter(aDoc);
136       for(; aFileIter.More(); aFileIter.Next()) {
137         string aURL = aFileIter.URL();
138         string aFileName = aURL.substr(aURL.find_last_of("/\\")+1);
139         int startUnderlineIndex = aFileName.find_first_of("_") + 1;
140         string aSourceFileName = aFileName.substr(startUnderlineIndex);
141         
142         //cout << "aURL = " << aURL << "\n";
143         string aPathToVault = aResponseFilePath.substr(0, aResponseFilePath.find("download")) + "vault/";
144         
145         //only for test only for WIN
146         //replace(aURL.begin(),aURL.end(),'/','\\');
147         //end only for test only for WIN
148         
149         //Get the files
150         //aGetFileRequest->setArgs0("http://" + aSimanWSHost + "/repository/" + aURL);
151         aGetFileRequest->setArgs0(aPathToVault + aURL);
152         aGetFileResponse = aStub->getFile(aGetFileRequest);
153         
154         outputStream.open((aClientFileDir + aSourceFileName).c_str(), ofstream::binary);
155         int aBufferSize = axutil_base64_binary_get_decoded_binary_len(aGetFileResponse->get_return(), Environment::getEnv());
156         int* aBufSize = new int[aBufferSize];
157         unsigned char* aCopyBuffer = axutil_base64_binary_get_plain_binary(aGetFileResponse->get_return(), Environment::getEnv(), aBufSize);
158         outputStream.write(reinterpret_cast<const char*>(aCopyBuffer),aBufferSize);
159         outputStream.flush();
160         outputStream.close();
161         delete[] aCopyBuffer;
162
163         //Set new URLs
164         aFileIter.SetURL((aClientFileDir + aSourceFileName).c_str());
165         //cout << "New location of the files " << aClientFileDir + aFileName << "\n";
166       }//aFileIter
167     }//aDocIter
168   }//actIter
169
170
171   //Delete objects
172   delete aCreateConfigFileRequest;
173   delete aCreateConfigFileResponse;
174   delete aGetFileResponse;
175   delete aGetFileRequest;
176   delete aStub;
177   
178   return aResult;
179 }
180
181 void SimanIO_Link::StoreConf(/*const*/ SimanIO_Configuration& theConf)
182 {
183   cout << "StoreConf method is started\n";
184   char* aSimanHost = getenv("SIMAN_WS_HOST");
185   if (aSimanHost == NULL)
186   {
187     CONFIGURSION_ERROR("The SIMAN_WS_HOST variable does not exist. Please set it.\n");
188   }
189   
190   string aSimanWSHost(aSimanHost);
191   string anEndpointUri = "http://" + aSimanWSHost + "/Siman-WS/services/SimanSalomeService";
192   
193   Environment::initialize("SimanSalomeService.log",AXIS2_LOG_LEVEL_DEBUG);
194   char* aClientHomeEnv = getenv("WSFCPP_HOME");
195   
196   if (aClientHomeEnv == NULL)
197   {
198     CONFIGURSION_ERROR("The WSFCPP_HOME variable does not exist. Please set it.\n");
199   }
200   
201   string aClientHome = AXIS2_GETENV("WSFCPP_HOME");
202   SimanSalomeServiceStub* aStub = new SimanSalomeServiceStub(aClientHome, anEndpointUri);
203   ServiceClient *client = aStub->getServiceClient();
204   
205   //Set the ServiceClient options
206   client->engageModule(AXIS2_MODULE_ADDRESSING);
207   Options *options = client->getOptions();
208   axis2_options_t *axis2options = options->getAxis2Options();
209   axis2_options_set_action(axis2options, Environment::getEnv(), "urn:putFile");
210   
211   //MTOM options
212   options->setTimeout(1000000);
213   options->setSoapVersion(AXIOM_SOAP12);
214   
215   //Upload the file onto server
216   PutFile* aPutFileRequest = new PutFile();
217   PutFileResponse* aPutFileResponse = new PutFileResponse();
218   
219   //Prepare salome-siman.conf file
220   string aTmpDir = "/tmp";
221   string aClientFileDir = aTmpDir + "/SimanSalome/" + myStudy + "/" + myScenario + "/" + myUser + "/";
222   //string aClientFileDir = aTmpDir + "/SimanSalome/" +  myUser + "/download/";
223   string aClientFilePath = aClientFileDir + "salome-siman.conf";
224   ofstream aSalomeSimanFile;
225   aSalomeSimanFile.open(aClientFilePath.c_str());
226   
227   //Get the actual file URLs and put these files onto server.
228   SimanIO_Configuration::ActivitiesIterator actIter(theConf);
229
230   for(; actIter.More(); actIter.Next()) {
231     SimanIO_Activity::DocumentsIterator aDocIter(actIter.Activity());
232     for(; aDocIter.More(); aDocIter.Next()) {
233       /*const*/ SimanIO_Document& aDoc = aDocIter.Document();
234       SimanIO_Document::FilesIterator aFileIter(aDoc);
235       for(; aFileIter.More(); aFileIter.Next()) {
236         string aURL = aFileIter.URL();
237         string aFileName = aURL.substr(aURL.find_last_of("/\\")+1);
238         cout << "aURL = " << aURL << "\n";
239         cout << " aFileName= " << aFileName << "\n";
240         //get size of the input file
241         ifstream inputStream (aURL.c_str(), ifstream::binary);
242         inputStream.seekg(0,ifstream::end);
243         int aSize = inputStream.tellg();
244         inputStream.seekg(0);
245         
246         //Upload action
247         char* aCopyBuffer;
248         aCopyBuffer = new char [aSize];
249         inputStream.read(aCopyBuffer,aSize);
250         axutil_base64_binary_t* aRequestDH = axutil_base64_binary_create_with_plain_binary(Environment::getEnv(), reinterpret_cast<unsigned char*>(aCopyBuffer), aSize);
251         inputStream.close();
252         delete[] aCopyBuffer;
253
254         //Set arguments
255         aPutFileRequest->setArgs0(aRequestDH);
256         aPutFileRequest->setArgs1(aFileName);
257         aPutFileRequest->setArgs2(atoll(myUser.c_str()));
258         
259         aPutFileResponse = aStub->putFile(aPutFileRequest);
260         
261         //Write information into salome-siman.conf file
262         ostringstream activityId;
263         ostringstream documentId;
264         activityId << actIter.ActivityId();
265         documentId << aDocIter.DocId();
266         aSalomeSimanFile << myScenario +  "," +  myUser + "," + activityId.str() +  "," + documentId.str() + "," + aPutFileResponse->get_return() + "\n";
267
268       }//aFileIter
269     }//aDocIter
270   }//actIter
271
272   aSalomeSimanFile.close();
273   
274   //Put the salome-siman.conf file onto server
275   ifstream inputStream (aClientFilePath.c_str(), ifstream::binary);
276   inputStream.seekg(0,ifstream::end);
277   int aSize = inputStream.tellg();
278   inputStream.seekg(0);
279   
280   char* aCopyBuffer;
281   aCopyBuffer = new char [aSize];
282   inputStream.read(aCopyBuffer,aSize);
283   axutil_base64_binary_t* aRequestDH = axutil_base64_binary_create_with_plain_binary(Environment::getEnv(), reinterpret_cast<unsigned char*>(aCopyBuffer), aSize);
284   inputStream.close();
285   delete[] aCopyBuffer;
286
287   //Set arguments
288   aPutFileRequest->setArgs0(aRequestDH);
289   aPutFileRequest->setArgs1("salome-siman.conf");
290   aPutFileRequest->setArgs2(atoll(myUser.c_str()));
291
292   aPutFileResponse = aStub->putFile(aPutFileRequest);
293
294   //Check in operation
295   axis2_options_set_action(axis2options, Environment::getEnv(), "urn:checkIn");
296   
297   CheckIn* aCheckInRequest = new CheckIn();
298   CheckInResponse* aCheckInResponse = new CheckInResponse();
299   aCheckInRequest->setArgs0(aPutFileResponse->get_return());
300   aCheckInRequest->setArgs1(atoll(myScenario.c_str()));
301   aCheckInRequest->setArgs2(atoll(myUser.c_str()));
302   aCheckInResponse = aStub->checkIn(aCheckInRequest);
303   
304   //Delete the directories
305   system(("rm -r " + aTmpDir + "/SimanSalome/").c_str());
306   
307   //Delete objects
308   delete aPutFileRequest;
309   delete aPutFileResponse;
310   delete aCheckInRequest;
311   delete aCheckInResponse;
312   delete aStub;
313 }