is this valid Factory pattern usage? - factory-pattern

I made PageFactory class for Paging the list of data regardless which DBMS is used.
But I'm not sure that this is valid factory pattern or something's wrong.
If there's better way to to this could you tell me?
package com.tource.cms.common.database.paging;
import com.tource.cms.common.environment.EnvironmentVariables;
public class PageFactory {
private static final String DEFAULT_DATABASE_TYPE = getDefaultDatabaseType();
public static Page createPage(int totalRow, int currPage, int blockRow, int blockPage) {
if("mysql".equals(DEFAULT_DATABASE_TYPE))
return new MysqlPageCalculator(totalRow, currPage, blockRow, blockPage).getPage();
else if("oracle".equals(DEFAULT_DATABASE_TYPE))
return new OraclePageCalculator(totalRow, currPage, blockRow, blockPage).getPage();
else {
try {
throw new UnsupportedDatabaseException();
} catch (UnsupportedDatabaseException e) {
e.printStackTrace();
return null;
}
}
}
/** getting DBMS type from cached Memory */
private static String getDefaultDatabaseType() {
return EnvironmentVariables.get("cms.jdbc.databaseType").toLowerCase();
}
}

As long as MysqlPageCalculator and OraclePageCalculator implement the same interface, or the same super class, let's say PageCalculator, you are implementing Abstract Factory pattern correctly.

As you have extract the creation process of page into a standalone class instead key word 'new'. So yes, it is a Simple Factory pattern.

Related

GATE: A working example for gate.corpora.DocumentJsonUtils

I have been trying to understand how to use the JSON exporter in GATE located in gate.corpora.DocumentJsonUtils. Can someone supply a working example? I am not quite sure where to find or how to construct the Map<String,Collection<Annotation>> annotationsMap which is required by all the methods.
Here is the "quick" hack that solved it for me. Not sure why they decided on this particular data structure. Also not sure why this is not done internally as a default option as it is derived from the document.
¯\_(ツ)_/¯
public static String makeJson(Document doc) {
AnnotationSet as = doc.getAnnotations();
Map<String, Collection<Annotation>> anns = new HashMap<>();
anns.put("MyAnnotations", as.inDocumentOrder());
try {
return DocumentJsonUtils.toJson(doc, anns);
} catch (IOException ex) {
return "";
}
}

Test case for Struts2 2.3.24 using Strut2SpringTestCase (request object is coming as null)

I am trying to write unit test cases for my Struts2 action classes. My Test class extends SpringStrutsTestCase class. I am able to set the request object and able to get the action and action is also getting called but when in action it tries to get the parameters set in request object it throws null pointer exception i.e. request object is going as null. Below is my what my test class looks like. Any help is really appreciated.
import org.apache.struts2.StrutsSpringTestCase;
import org.junit.Test;
import com.opensymphony.xwork2.ActionProxy;
public class testClass extends StrutsSpringTestCase {
#Test
public void test1() throws Exception {
try {
request.setParameter("p1", "v1");
request.setParameter("p2", "v2");
ActionProxy proxy = getActionProxy("/actionName");
MyActionClass loginAction = (MyActionClass) proxy.getAction();
loginAction.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public String[] getContextLocations() {
String[] arr = new String[] { "one.xml", "two.xml", "three.xml" };
return arr;
}
}
Here is my action class.
public class MyAction extends ActionSupport{
private String p1;
private String p2;
/*
Gettere and Setters of p1 and p2
*/
public String execute() throws Exception {
// return "success";
logger.info("Login Action Called");
String pv1= (String) request.getParameter("p1");// If I get value using this.pv1 it works fine but with this code it doesn't.
String pv2= (String) request.getParameter("p2");
return "success";
}
}
In order to test an action call you need to call execute method of ActionProxy. By calling execute of your action you are just invoking that particular method of the action class and not S2 action along with the interceptors, results, etc.
The correct way would be:
ActionProxy proxy = getActionProxy("/actionName");
proxy.execute();
BTW if you're using JUnit 4 there is StrutsSpringJUnit4TestCase which you should use instead of StrutsSpringTestCase.

Need to sort a list using Wicket

I am working on a very simple program, looking like this:
public class WicketApplication extends WebApplication implements Comparable<Object>{
private List<Person> persons = Arrays.asList(
new Person("Mikkel", "20-02-91", 60169803),
new Person("Jonas", "02-04-90", 86946512),
new Person("Steffen", "15-07-90", 12684358),
new Person("Rasmus", "08-12-93", 13842652),
new Person("Michael", "10-10-65", 97642851));
/**
* #see org.apache.wicket.Application#getHomePage()
*/
#Override
public Class<? extends WebPage> getHomePage() {
return SimpleView.class;
}
public static WicketApplication get() {
return (WicketApplication) Application.get();
}
/**
* #return #see org.apache.wicket.Application#init()
*/
public List<Person> getPersons() {
return persons;
}
public List<Person> getSortedList(){
return Collections.sort(persons);
//This won't work before implementing comparator i know, but how??
}
#Override
public void init() {
super.init();
// add your configuration here
}
#Override
public int compareTo(Object o) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
That was the class where i just put my people into a list.
public class SimpleView extends SimpleViewPage {
public SimpleView() {
ListView persons = new ListView("persons", getPersons()) {
#Override
protected void populateItem(ListItem item) {
Person person = (Person) item.getModelObject();
item.add(new Label("name", person.getName()));
item.add(new Label("birthdate", person.getBirthdate()));
item.add(new Label("phone", person.getPhone()));
}
};
add(persons);
add(new Label("size", "Number of people " + getPersons().size()));
}
}
And here is what i do with the people.
Basicly i want the program to show a table with all the data(this already works).
Now i want to be able to sort them. But i can't for the life of me figure it out. I'm still rather new at programming, and i want to have a button below my table that can sort on name, bday or phone number. Was thinking about trying to Comparable, but can't remember it that well, and not sure how it works with Wicket..
Thanks for the help in advance :)
What you need is the DataView component, which provides all the support you need for sorting (and paging, should you require it later on).
Here's a working example, if you click on the "Source Code" link in the top right corner, you can see that most of the things you want from a sortable table work out of the box. All you need is to create a suitable data provider.
If you use DataView with a SortableDataProvider, you don't need to worry about writing your own dynamic Comparator. (Which is not a terribly hard task itself, but it's easy to get it wrong.)

How to send a property as a string in RestEasy response?

I have a class called Product which has a property called id of type long. Below is the class
public class Product {
private long id;
}
The value of id is beyond the value which javascript can handle. I realized this after seeing the below link
Parse json in javascript - long numbers get rounded
I dont want to declare the field as String in the domain class. But I want to say to RestEasy that it has to send the value as a string in the json response.
How can I do this? I dont want to use any third party api. Is it possible in RestEasy. I have gone through the documentation but did not find any such annotation or may be I did not go through the documentation properly.
Can anyone please help. Thanks all in advance.
If you are using Jackson as JSON Serializer you can extend the JacksonJsonProvider:
#Provider
public class JsonProvider extends org.codehaus.jackson.JacksonJsonProvider {
public JsonProvider() {
ObjectMapper objectMapper = locateMapper(ObjectMapper.class, MediaType.APPLICATION_JSON_TYPE);
objectMapper.configure(org.codehaus.jackson.JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true);
}
}
If you are using Jettison you can register a custom XmlAdapter:
public class LongAdapter extends XmlAdapter<String, Long> {
#Override
public String marshal(Long id) throws Exception {
if (id == null) {
return "";
}
return id.toString();
}
#Override
public Long unmarshal(String id) throws Exception {
return Long.parseLong(id);
}
}

wicket persistent object between panels

In wicket without saving to the session how can i have a persistent object for example a list which can be set in one panel and accessed from another. Iv done a lot of googleing and im not entirely sure how this would work. Any help would be appreciated greatly! Thank you.
Related to the comments above, I will try and explain what I was thinking.
Disclaimer: It's been more than a year since I worked with Wicket, so the following should be read as an overall proof-of-concept. I cannot guarantee that it will compile (actually, I can almost certainly guarantee that it will not.)
public class MyPage extends ... {
...
MyPageModel pm = new MyPageModel();
add(new MyPanel1(pm));
add(new MyPanel2(pm));
...
}
public class MyPageModel implements Serializable {
private IModel<List<MyDataObject>> dataObjects;
public MyPageModel() {
this.dataObjects = // Load list from somewhere
}
public IModel<List<MyDataObject>> getDataObjects() {
return this.dataObjects;
}
}
public class MyPanel1 extends ... {
private MyPageModel pageModel;
public MyPanel1(MyPageModel pageModel) {
this.pageModel = pageModel;
...
add(new ListSomethingComponent<MyDataObject>(pageModel.getDataObjects)); // Some list renderer component which takes a IModel<List<MyDataObject>> as data
}
}
public class MyPanel2 extends ... {
private MyPageModel pageModel;
public MyPanel2(MyPageModel pageModel) {
// Same as MyPanel1...
}
}