Web services are web application components. Web services can be published, found, and used on the Web.
Web services allow different applications from different sources to communicate with each other without time-consuming custom coding
Over here we will see how to use controllers to insert data from outside sources using Web Services.
Below are the steps to create a web service:
- Go to your project Src --> Right click Customization–> new–> other
- Select class--> Next
- New java class window will open
- Name the class and refinish
- NewMyItem class will be created in the project explorer.
- In the MyItem class, we will write the following code.
- Explanation of the code:
- MyItem class acts a gateway to bring data from the web to our project.
- The class should implement Serializable (i.e.java.i.o.Serializablemethod)
- You can use Refactor–> Encapsulated field in Eclipse, It automatically creates
get and set values for your objects.
MyItemClass
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 | package com.EasyLPT_forCZRT.customization; import java.io.Serializable; //Implements Serializable public class MyItem implements Serializable { public String code; public String description; private String auxCode; // Get and Set the data from the MMBOController public String getCode() { return code; } public void setCode(String code) { this .code = code; } public String getdescription() { return description; } public void setDescription(String description) { this .description = description; } public String getAuxCode() { return auxCode; } public void setAuxCode(String auxCode) { this .auxCode = auxCode; } } |
- Here in MMBOController we create the object of the class MyItem and create a
constructor inside that object. - As we already passed the parameters to the object so in constructor we are just regenerating
them. - As we already set the get and set, we will get the data to the constructor.
MMBOController
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 | package com.EasyLPT_forCZRT.customization; import com.lbs.control.interfaces.ILbsController; import com.lbs.controllers.ILbsControllerTask; import com.lbs.controllers.gen.mm.MMXFItemBrowserController; import com.lbs.controllers.gen.mm.MMXFItemController; import com.lbs.controllers.menu.LbsMenuController; import com.lbs.remoteclient.IClientContext; public class MMBOControllerSample implements ILbsControllerTask { //Create an object of the class MyItem private MyItem localItem = null ; public MMBOControllerSample(MyItem myItem) //constructor { localItem = myItem; } public void execute(LbsMenuController menu, IClientContext context) { try { ILbsController controller = menu.launchMaterials(); if (controller instanceof MMXFItemBrowserController) { MMXFItemBrowserController controller1 = (MMXFItemBrowserController) controller; controller = controller1.createNew_CGCommercialGood(); if (controller instanceof MMXFItemController) { MMXFItemController controller2 = (MMXFItemController) controller; controller2.setCode( "MyCODE2" ); //with the help of get and set method we are importing the data here into controller. controller2.setDescription(localItem.getdescription()); controller2.selectCheckBox_TrackbyLocation(); controller2.selectCheckBox_DiscountInapplicable(); controller2.selectCheckBox_Configurable(); // controller2.setGroupCode(""); controller2.setSearchText( "mytext" ); controller2.setAux_Code(localItem.getAuxCode()); // controller2.setAux_Code(""); controller2.clickSave(); } } } catch (Exception e) { //TODO handle your exceptions } } } |
- CreatingCustomWebServiceclass
- There are two ways to create a new web service:
1) Under your project go to Definitions --> Web Services.lws as shown in Fig 1
2) Under your project go to ProjectDefinitions.ldef as shown in Fig 2
- Both the methods you will get this window
1) Right Click on the window, you will see an Add button
2) You will get a Web Service Definition Window
- Give name to your service and description
- Browse your WebService Main Class
- Press Ok and your web service class is created
For example, we created WebServiceTest
- After you deploy to check if your service is added to the Services or not follow the steps:
1) Go to logo project
2) Open WEB-INF
3) Services - You will see your service added in the list
- Under the CustomWebService we write the following code.
- We will work on Client Context and server context.
- To create a Server Context
We need 3 parameters (Firm number, period number, language) - We will call (item) in the MMBOController
- the button that we created in the SampleController pass the
execute controller that calls the Sample which generates the material - IServerContext: is the properties of the environment that we read
(Eg. firm No, period number, language).We have to create the object on ServerContext in
the execute Controller. - We need to generate the ServerContext as we are outside the environment so to activate
we need the parameters(Firm number, period number, language) - Put it under try-catch
- ExchangeResult is a method that would let us know if the web services is imported properly
or there are some errors.It gives us a report.
CustomWebService
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 | package com.EasyLPT_forCZRT.customization; import java.util.ArrayList; import javax.jws.WebService; import org.webharvest.definition.TryDef; import com.lbs.controllers.LbsControllerUtil; import com.lbs.data.factory.FactoryParams; import com.lbs.data.factory.IObjectFactory; import com.lbs.data.objects.CustomBusinessObject; import com.lbs.data.query.QueryBusinessObject; import com.lbs.data.query.QueryBusinessObjects; import com.lbs.localization.LbsLocalizableException; import com.lbs.platform.interfaces.IServerContext; import com.lbs.remoteclient.IClientContext; import com.lbs.util.QueryUtil; import com.lbs.ws.ExchangeResult; import com.lbs.ws.WebServiceHelper; import com.lbs.xui.customization.JLbsXUIControlEvent; public class CustomWebService { IServerContext context; //ExchangeResult is a data object which is already defined in the library //parameters for server context public ExchangeResult createMMBOItem( int firm, int period, String language, MyItem item) { MMBOControllerSample sample = new MMBOControllerSample(item); CustomBusinessObject cbo = new CustomBusinessObject(); // ServerContext is the properties of the environment that we call through controllers IServerContext serverContext = null ; try { serverContext = WebServiceHelper.getContext(firm, period, language); // button that we created pass to executeController which task the Sample LbsControllerUtil.executeControllerTask(serverContext, sample); } catch (LbsLocalizableException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return new ExchangeResult(); } |
Copyright © 2018 Logo Yazılım