Wednesday, 17 September 2014

Servoy Web Services Part-4

Hello and welcome back... :)

Hope by this time you must have successfully posted a form and save the data in database...
Now let move ahead and GET those data and display in our web page.

To fetch data from Servoy one must have to do a GET request through HTTP. 
Let us add a HTML Button to send the request. We can request for a particular record, bunch of records or all the records. So let us put a text box as well to to enter the ID to be fetched.

 <label for="name">Enter ID to get the value:<br></label>
 <input name="elementId" type="text"  /><br><br>
 <input type="button" value="GET" onclick="getServoyDataInForm();"/>

Definition of  getServoyDataInForm()...



function getServoyDataInForm(){
// Fetch the value from the text box
var id = $("input[name='elementId']").val();
// If we have a value the send request with a value else go for all the values
if(id){
$.ajax({url:'http://ip:port/servoy-service/rest_ws/servoy_web_services/servoyWebServiceMethods/'+id,
type:'GET',
success:function(data){
 alert("Data: " + data.firstName);
},
error : function(){
alert('Fail to retrive data');
}
});
} else {
$.ajax({url:'http://192.168.9.137:8087/servoy-service/rest_ws/servoy_web_services/servoy_web_services/',
type:'GET',
success:function(data){
 alert("Data: " + data);
},
error : function(){
alert('Fail to retrive data');
}
});
}
}

 Servoy's ws_read(data)...


/**
 * Handle GET request
 *
 * @param {Number} [getValue]
 *
 * @returns {Object} servuyUser
 *
 * @properties={typeid:24,uuid:"2b108b10-b103-414f-8baf-7d7bee6c1034"}
 * @AllowToRunInFind
 */
function ws_read(getValue)
{
// Load as per the requested ID
if (getValue) {
if (foundset.find()) {
foundset.survey_id = getValue;
if (foundset.search()) {
var surveyUser = {};
surveyUser.id = foundset.survey_id;
surveyUser.firstName = foundset.firstName;
surveyUser.lastName = foundset.lastName;
return surveyUser;
}
}
} else {
// Load all the records
if (foundset.loadAllRecords()){
var surveyUsers = [];
for (i = 1; i <= foundset.getSize(); i++) {
surveyUsers[i] = foundset.getRecord(i).firstname;
}
return surveyUsers;
}
}
// Search failed
return null;
}
This is it..we will be getting a alert with the details of a user if we pass a value as GET request and if we do not send anything we will be getting a alert will all then first names...

By now you must have got an idea for how to handling the request in servoy and web page...So what I will be doing is to put the rest two request's code snippet... Making it easier to read though..and saving for typing for me.. :P

Update(PUT) request :
JS Method :

function updateServoyDataInForm(){
var id = $("input[name='elementIdtoUpdate']").val();
var formData = {};
jQuery('form').serializeArray().map(function(item) {
formData[item.name] = item.value;
});
if(id){
$.ajax({url:'http://ip:port/servoy-service/rest_ws/servoy_web_services/servoyWebServiceMethods/'+id,
type:'PUT',
data: JSON.stringify(formData),
success:function(data){
alert("Data updated successfully");
},
error:function(){
alert("Data update failed");
}
});
} else {
alert("Enter an element ID to update");
}
}

ws_update(data):

function ws_update(updatedData, id){
// update 1 employee
if (updatedData && id && foundset.find()){
foundset.survey_id = id;
if (foundset.search()){
var record = foundset.getRecord(1);
if(updatedData.id && id != updatedData.id){
record.survey_id = updatedData.id;
}
if(updatedData.firstName){
record.firstName = updatedData.firstName;
}
if(updatedData.lastName){ record.lastName = updatedData.lastName;
} return databaseManager.saveData(record); // response code 200/404
}
}
// not found or cannot search
return false; // response code 404
}


Now lets delete a user...Be careful its now removing data from database...handle it with care and add some security..of course...(I will be add that in another article)

Delete Request :
JS Code:

function deleteServoyData(){
var id = $("input[name='elementIdToDelete']").val();
$.ajax({url:'http://ip:port/servoy-service/rest_ws/servoy_web_services/servoyWebServiceMethods/'+id,
type: 'DELETE',
success: function(result) {
alert(result);
}
});
}

ws_delete():

 function ws_delete(id){

if (id && foundset.find()){
foundset.survey_id = id;
if (foundset.search()){
return foundset.deleteRecord(1); // response code 200/404
}
}
// not found or cannot search
return false // response code 404
}

This completes the CRUD action performed on Servoy with the help of Web Services...

Will be adding the security and what else we can add in web services in some time..

Hope this will be useful to members in Servoy community..Please give you suggestion and scopes for improvements...

Thanks for being with me...


Happy Coding..:)

3 comments:

  1. Is that necessary that solution in which web service methods written should be active and running at moment when we call web service method..?

    ReplyDelete
    Replies
    1. Hello Prasad,

      No, it is not required.
      When we start the application server, the solutions are made available to be accessed through web-services. The application server must be up and running.

      You can test by doing a POST request, from a web page just after starting the server(without opening any client). You will get a response.

      Hope this helps,

      Thanks Sovan

      Delete
  2. Hiring an SEO provider should be seen as an investment in your business. You should not view it as a business expense, but rather a business strategy and an effective way of enhancing your business presence within your business sector. Try not to begin your search with the intention of "buying some SEO". Hiring an SEO provider should be viewed rather as hiring an employee that understands and cares about your business and its online objectives.Blog Comment

    ReplyDelete