Spring and a Swing application together? - java

Our assignment for our java project is to make a tool for kids to make math excercices. One part should be in a swing application, where the teacher can adjust settings to what the kids should make, view their results, etc...
The other part is where kids should be able to make excercices on the internet.
Now, so we thought, as we are seeing Spring in Java courses now (just starting to.).Let's make it a Maven project, and reuse the service layer + DAO, and use the same model. That way the desktop app doesn't have to use the Spring framework neccessarely. (So we thought...)
We came to the conclusion that we don't know enough about MVC to pull this off.The service layer always returns the modified object that was saved in the database after executing business logic. Now this doesn't really work with using MVC in swing (or please tell us how to use MVC the right way..), As, as we see it, the controller modifies the data while the view receives an update of the model (via observer). But that object is replaced by a total new one!
Could please someone help us out of this, or give some tips how to fix this? Double linking controller and view doesn't seem a good idea to us at all, so is there a way to fix this, or would you recommend us to go Spring all the way, even if we have yet to learn this and only have roughly 3 months to make this?

Create a layer of Model class pojos.
Set them from swing or your web app and directly pass it to Service layer[Spring module in your case] and operate on it.
On WebApp we have scope like request,session, for swing you need to maintain it manually.

Related

Where to put #Transactional and the logic which invokes two or more tables?

I am currently developing a web server using Spring Boot.
Since I am a beginner in Spring Boot and server and I have no developer around me,
I am not sure if I am doing all right or all wrong.
My project structure on Spring Boot server is like this.
As you see there are six entities, which are tables in MySQL.
Each of them has its controller, service and repository. (and the repositories implements JpaRepository)
Clover is a lottery name like Powerball.
If a user buys a clover, that clover should be added to a purchased clover list which is CloverValid in the picture, and user's point has to be decreased by 10.
So, when a user clicks a button on a website or an android app,
the server has to update the user's point in User table,
and insert a value of new clover - his id and a timestamp.
And of course those two operations should be transactional.
I want to know where to put this logic.
I think it's service so should I add a new service class or
change the whole structure?
If I do have to change this structure and start again
please tell me how or give a little advice or tip for me.
Thank you and I am really sorry if I am violating any rules of Stack Overflow without knowing it.
A simple structure could be entity driven, in your case UserController/UserService/UserRepository/User.
Transactional boundaries belong to the service layer, you can use Spring annotations and use them on public methods.

Web based worklow manager

I've got a framework that i wrote, which uses dependency injection in order to instantiate a service with a particular data processing workflow. I don't want to have to write a huge spring file every time i want to use it so i would like to be able to draw the workflow in a diagraming tool where i can set properties and such and then take that diagram and generate the spring config from it. it's akin to the some IDE's where you draw stuff and set properties. I realise there may not be something built for this but if you could point me in the direction of a collection of tools that i can use to accomplish this or terminology that i can search the web for or anything that would be useful in making this happen, then i would really appreciate it.

MVC and Swing in desktop application

After realizing that I have completely ignored the MVC pattern I have tried to utilize the concept in an application with a Swing view. I have now read most of the posts on the subject of MVC with Swing but am still a bit confused, because it is too complicated for me to grasp, and I think I need some basic clarifications so I don't set off on the wrong path.
I also wonder how common it is to use MVC in real projects. Many online tutorials seem to leave out the controller and mix it with the model, while I was confused by XSTL:s business logic capabilities. Why would you want to address a datasource from a JSP view?
These thoughts aside, my proper question is this:
If you have a Swing component, should event listener in that Swing class update the component state through calling (static perhaps?) methods in a POJO controller class, which in turn gets the appropriate business logic from the model, which is made up by POJO class hierarchy and associated persistence?
I've worked as a freelance for a long time and almost 90% of the projects were about Java Swing (Desktop applications). Also a lot of projects involved migration from languages like Visual Fox Pro to Java, it was a pain, because the hard part is not think in the logic which is already done, the hard part is take the code that is a mess and turn it into a good-looking code following the good practices and using design patterns, that's why it is a good idea to make a schema or a map in your mind how you can separate your code following the concepts of Model, View, Controller.
MVC as mentioned helps you to have a good-looking, maintainable and easy to read code, as well as you follow the programming paradigms and good practices.
View: Obviously, the part that interacts with the user (user interface), in case of Swing, your windows, frames, panels and all the code that involves the graphic components you need for your app.
Controller: Involves the core or business logic you stablish for your application, in this "layer" you should include the functionality and the "how my application will achieve the goals?".
Model: Related with the data you manage, for example, your entities and classes that represents the data you want to manage or give maintenance.
Applying MVC is not so hard, but as I mentioned, it could be sometimes a pain when you have to migrate your code from a not-applying-MVC structure to a MVC structured application. It is easier to start coding using MVC.
A way I get used to it is by using maven and separate my application into little "modules", of course, you don't need maven, I just found it useful in that moment, but in any case you can try practicing or get used to MVC by separating your application into little projects, for instance:
Java Project 1: application-data-model (contains all the code related with data management: entities, dtos, beans, daos)
Java Project 2: application-core-controller (contains all the business logic and functionality, you can use a facade pattern here if you want to make your code more "transparent" when you relate with your view)
Java Project 3: application-view-ui (contains all the panels, frames and graphic components)
Working this way helped me (and forced me) to get used to separate my code and keep an eye on what really matters to the project I'm working on. For instance, if I'm on application-data-model I'm focused in data model, I'm not thinking in business logic nor graphic interface.
Long explanation, maybe somebody could do it better, but hope I could have helped you or at least gave you a hand with this.
Best regards.
Firs the URL for basic understanding of MVC
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
Now the approach to implement it in Swing applications. Don't get confused with controller functionality with the listeners functionality.
UI controls and listeners attached to them should be defined in your view classes.
On any event, whenever you need to invoke a business logic, then you need to call the controller class. Like fetching some value from the database.
Controller class should talk to your model to fetch the data and manipulate it if required.
Model classes should work on the data.
The idea of using MVC is to reduce redundant code and more manageable code. So if you are doing some calculations/manipulations then those can be moved to Controllers. Controllers can be called from different views requiring the same stuff. Similarly model can be used by multiple controllers to fetch the data.

How to organize controllers/presenters in a large JavaFx 2.0 application?

For a project I've been working on a JavaFX 2.0 desktop application (a keytool UI). The way JavaFx2.0 works (in my project anyways), the UI event handling takes place in the JavaFX 2.0 UI classes itself (for example: onclicked() events or property change listeners).
Right now I use a static class with a method: getController(), which all UI classes use to access the one controller of the application (somehow it seemed messy to me to pass the controller on to all 50+ UI classes).
The problem is however that that one controller is getting very large! It has way too many methods (all business logic methods that need to be accessed by my UI classes). Even though it only passes the methods calls on to my model/service, there is still a lot of exceptions that need to be caught on controller level for handling them in the UI (show error messages etc).
Anyone know of a clean way to make this whole MVC/MVP pattern work better for my application without the UI / Controller / Model classes being directly depending on eachother? Maybe a different controller for each use case? But then how would I make it so that the right UI class gets the right Controller without directly knowing it? Maybe using an interface?
I don't really know Java FX, so you should take my answer with a grain of salt. I looked a bit at Java FX tutorials, but they all seem to be tiny examples with no architecture of any kind... MVC or other.
Maybe you should not try too hard to have a clean MVC pattern. It seems to me that each UI element is itself a MVC unit, e.g. a Label contains text (the model), its graphical representation (the view) and it handles events (the controller).
You might just be making your life more painful by trying to have a separate global controller.
You might however keep a separate model, which would be necessary for example if you are showing the content of a database (the model). However if you are doing something quite simple, just using the state of the UI as your model would be sufficient; keeping the data (model) separately in the program would just make you waste time synchronizing the data in the UI's and the data in the separate model. Data duplication is evil and should be avoided as much as possible.
I propose you to test the JRebirth Framework
It provide a simple but powerful pattern which will help you to structure your application.
This framework is young and will be improved according to feedback received. Don't hesitate to send some.
http://www.jrebirth.org/doc/Overview.html
Check the overview page and source code provided to learn more.
Live Demo are also available
There is a series of blogs here Building JEE applications in JavaFX 2.0 that may help you. It presents several patterns with example on how to decouple the different components (MVP) in an JavaFx2 application.
I have put a basic tutorial on my website for a while ago on how MVC pattern can be implemented using Java and Javafx 2. The model class is always decoupled and shouldn't know anything about the controller and viewer. If this is a large project I would recommend using modules where you have your model, viewer and controller in there. A module could be admin portal or google map viewer etc.
http://www.pergande.net/blog/article/post/Javafx+2.0+MVC/id/1

Java, moving from desktop app to web app

I'm going to write my first Java based web app, and I'm sort of lost how to begin.
Firstly, I would like a web app and a desktop app that do pretty much the same thing, without the hackish idea of embedding a web browser into the desktop app because that doesn't allow to easily make changes to the desktop without affecting the web app and vice versa.
Now, here my questions.
Right now, I have a bunch of POJOs and they communicate with a single class that, right now, uses a flat file as a "database", of course, in production, I would use a legitimate database and just change that single class. Is this a good idea? Will I be able to go from POJOs to a web app?
Should I use a framework? I would like to have this app written pretty soon, seeing that all the buisness logic is there, I just need to wrap it so its usable, so, I don't want to spend an extreme amount of time learning, say, Spring (which AFAIK is huge), but, I don't want to keep reinventing the wheel throughout my app either. I can always just use JSP and scriptlets...
If you said yes to the above, what framework(s) do you suggest? Please note that I would like a framework that I can start using in maybe 3-4 weeks of learning.
Will I have to start from scratch with the POJOs that I have written? They're well over 30k LOC, so, if it is like that, I'll be hesitant.
You will need:
a web framework. Since you have Swing background, JSF 2 will be your best bet (everything will be painful, of course, but JSF will get you up and going quickly and will help you avoid the most tragic mistakes). Also, wrapping business pojos into web guis is the main use-case for JSF and it's biggest focus.
a "glue framework". One thing that is much different with web applications as opposed to desktop ones is that you cannot create view components by yourself - they must be created when browser requests a page. So you have to find a way to create the view objects and deliver all the references to the pojos that represent logic, some of which may have very different lifecycles (this is not a problem on desktop, but on web you have to distinguish between pojos that live along with the whole application, along with a single user session, along with a single request, and so on).
The "glue framework" could also provide the additional benefit of managing transactions. You have three choices:
Spring. It's not half as complex as you thing; you only need to learn some basic stuff.
EJB. You would need a real application server, like Glassfish or JBoss
bare JSF has good support for dependency injection, the only drawback is the lack of automatic transaction management.
If I were in your position, I would go with bare JSF 2.0 - this way you only need to learn one new technology. At first, try to avoid libraries like PrimeFaces - they usually work worse than advertised.
edit - and addendum
or - what is "dependency injection"(abridged and simplified)
When request comes to a web application, a new task starts in a new thread (well, the thread is probably recycled, but that's not important).
The application has already been running for some time and most of the objects you are going to need are already built and should not get created again: you have your database connection pool, maybe some parts of business layer; it is also possible that the request is just one of many request made during one session, and you already have a bunch of POJOs that the user is working on. The question is - how to get references to those objects?
You could arrange your application so that resources are available through some static fields. They may be singletons themselves, or they could be acquired through a singleton locator. This tends to work, but is out of fashion (hard to test, hard to refactor, hard to reuse, lifecycles are hard coded in application). The real code could look like this:
public void doSomething() {
Customer Service cs = AppManager.getInstance().getCustomerService();
System.out.println(cs.getVersion());
}
if you need clustering and session management, you could build a special kind of broker that would know and provide to anyone all kinds of needed objects. Each type of object would be registered as a factory under a different name. This also works and is implemented in Java as JNDI. The actual client code would look like this:
public void doSomething() throws Exception {
CustomerService cs = (CustomerService)new InitialContext().lookup("some_fancy_looking_name_in_reality_just_string");
System.out.println(cs.getVersion());
}
The last way is the nicest. Since your initial object is not created by you but by the server just after http request arrives (details depend on the technology you choose, but your entry point might be a JSF managed bean or some kind of action controller), you can just advertise which references you need and let the server take care of finding them for you. This is called "Dependency Injection". Your acts as if everything is taken care of before your code is ever launched. Spring or EJB container, or CDI, or JSF take care of the rest. The code would look like this (just an example):
#EJB
CustomerService cs;
public void doSomething() {
System.out.println(cs.getVersion());
}
Note:
when you use DI, it really uses one of the two former methods under the hood. The good thing is: you do not have to know which one and in some cases you can even switch them without altering your code;
the exact means of registering components for injection differs from framework to framework. It might be a piece of Java code (like in Guice), an XML file (classic Spring) or an annotation (classic EJB 3). Most of the mentioned technologies support different kinds of configuration.
You should definitely use a framework as otherwise sooner or later you'll end up writing your own.
If you use maven then simply typing mvn archetype:generate will give you a huge list of frameworks to choose from and it'll set up all of the scaffolding for you so you can just play with a few frameworks until you find the one that works for you.
Spring has good documentation and is surprisingly easy to get started with. Don't be put off by the pages of documentation! You could use JPA to store stuff in the database. You should (in theory) just be able to annotate your existing POJO's to denote primary keys and so on and it should just work. You can also use JSP's within Spring if that makes life easier.
... I a bunch of POJOs and they communicate with a single class that, right now, uses a flat file as a "database", of course, in production, I would use a legitimate database and just change that single class. Is this a good idea? Will I be able to go from POJOs to a web app?
qualified yes. if the pojo's are sane you should not have many problems. many people use hiberbate.
Should I use a framework? I would like to have this app written pretty soon, seeing that all the buisness logic is there, I just need to wrap it so its usable, so, I don't want to spend an extreme amount of time learning, say, Spring (which AFAIK is huge), but, I don't want to keep reinventing the wheel throughout my app either. I can always just use JSP and scriptlets...
probably. spring is huge, but things like grails or roo can help.
if you want to have a responsive web app, you will need to do some kind of rich client (AJAX). this may require a lot of your code to run on the client. this means writing a lot of javascript or using gwt. this will be a pain. it probably will not be so easy to just "wrap it". if you have written a swing app, then basically that code will need to run on the client.
If you said yes to the above, what framework(s) do you suggest? Please note that I would like a framework that I can start using in maybe 3-4 weeks of learning.
i like groovy and grails - grails uses spring-mvc, spring, hibernate. but there is roo, play and others.
Will I have to start from scratch with the POJOs that I have written? They're well over 30k LOC, so, if it is like that, I'll be hesitant.
the code that will run on the server can probably be mostly left alone. the code that has to run on the client needs to be rewritten in javascript or maybe you can get some reuse out of that code by using gwt,
The Play Framework is doing great things. I would recommend it highly. Having worked with EJB apps and Tomcat/Servlet/Spring apps it's a breath of fresh air. After framework installation you get a working app in a few seconds. Reminds me of Ruby on Rails or Node.js with the type-safety of Java.
Much quicker turnaround on getting started, faster development cycles, and a clearer configuration model than previous Java web app frameworks.
http://www.playframework.com/

Categories