Friday, July 31, 2015

Create a Download link with javaScript (Front End Stuff)

If you want to have some content fetched by JavaScript you just need to do as follows:
Download File

Now this is simple create the content hardcoded but you can do whatever you want, here is an example on getting content from server download the file
          ${responseItem.xmlRequest} 

          Download Request
          


This is a live demo, just click HERE to download a file generated as the above code
Is worth to mention that if you have spaces on your test you need to encode them, otherwise those will disappear as in the download file above. The reference for this can be found at http://stackoverflow.com/questions/7717851/save-file-javascript-with-file-name

Get Current method name (Java)

There are classes that it is important to debug when a method starts and when a method ends.
Since there will be several methods you can get the mehtod name with java code so you just use the same line to log where your code is being executed.

in order to do this, we need to get the method and the class name for the current method

This is done by calling the currentThread Statck so we can get the name for the method as follows:


As you can see the currentThread().getStackTrace()[2] will be the thread that called getCurrentMethodLogMessage() that is why is on index [2]

In order to call this you can do as follows:

You can get a reference for this at http://stackoverflow.com/questions/442747/getting-the-name-of-the-current-executing-method

Monday, July 27, 2015

Set property values to dynamo components (ATG Oracle Commerce)

There are several ways to set values to dynamo components that are not just simple String, Boolean, Numbers or Components.

Set a Property to point to another component
commerceItemManager^=/atg/commerce/order/purchase/PurchaseProcessHelper.commerceItemManager

Define a List of values:
requiredAddressPropertyNames=firstName,\
                             lastName,\
                             address1,\
                             city,\
                             state,\
                             country,\
                             postalCode,\
                             phoneNumber

Define a Map, which is similar to the list
addressPropertyNameMap=firstName=First Name,\
                lastName=Last Name,\
                address1=Street Address,\
                city=City,\
                state=State,\
                country=Country,\
                postalCode=Zip Code,\
                phoneNumber=Phone Number

Set Null using the Constants component
relRankStrategy^=/Constants.NULL


All of this can be found on ATG Oracle Commerce documentation:

Tuesday, July 21, 2015

Grails domain class response as json (Grails)

With a grails Controller you can response your Domain class objects as json response.
it is as easy as follows:

Let's create a Product domain class as example

  Then run grails generate-all com.ocorp.grails.Product

 This will generate all our controllers, views, test cases.

 Inside our ProductController let's create a list() method or name it as you like better, and reneder the contest as json as follows:

When you go to /yourAppContext/product/list the response will be json for all the elements for your Domian Class Product:


This can be found at grails documentation http://grails.github.io/grails-doc/2.3.4/guide/theWebLayer.html#xmlAndJSON


Wednesday, July 15, 2015

Difference between doStopService and stopService on GenericService components (ATG Oracle Commerce)

It is common that when you are working with a GenericService component you need to change properties on it, and those are not reflected when the service runs after the changes are made, for those cases you need to stop the service and start it again.

There are 2 methods doStopService and stopService.

So, what should I do, just doStopSerivce?, run both? if so in which order?



According to the API, here is what each method does.

stopService

public void stopService()
                 throws ServiceException
Stops the service. This will be called when the Service is being shut down, either by explicit instructions from the administrator, or for various other reasons. A Service should clean up any resources it is using, but should be prepared to restart itself. When asked to restart, its properties may have changed.

doStopService

public void doStopService()
                   throws ServiceException
This is called when a Service is required to shut down. The Service should respond by stopping any processes that it has started.

So here are the answers for the questions above:

You just need to run stopService, this will call doStopService on it, and it will do things like change running flag to false and clean up resources, meaning your properties changes will be grabbed next time the service Starts.


Wednesday, July 8, 2015

Avoid java verbose code for Setters and Getters with anotations (Java)

There are times where your class needs to have several properties along with their setters and getters, this is ok, you can just auto generate them with eclipse, but your code at the end of the day will be full of a lot of verbose lines of code for those Getters and Setters.

In order to avoid this there is a Project called Lombok, they made a library to create Setters and Getters with Java annotations, at the beginning I was skeptical about this, but when I implemented on our Enterprise application, realized that it works as expected, and the good thing is that is integrated with eclipse out of the box, and the only requirement for your project is add this on your Java classpath.

The code looks like this, eclipse compile the code for you and display the generated setters and getters on the outline for you.


Just take a look at  Lombok and check the Demo Video, setters and getters are just a single feature, they have more that this.

I have used this inside my ATG application, just needed to add the .jar into MAINFEST.MF along with the Java classpath.