In a Struts application some controllers write Javascript code to the HTTP response (this JS code will be executed when the submit ends). Do you think it's a bad approach or is acceptable ? Would it be better to set an attribute and move the JS code to the view ?
#UserInterfaceEvent
public void aceptar_onClick(FormActionContext ctx) {
IGenerateDocListOutputBF bproxy = ProxyFactory.newBusinessProxy(getBusinessComponent(), IGenerateDocListOutputBF.class);
GenerateDocListOutputForm form = (GenerateDocListOutputForm) ctx.form();
String mode = form.getDetailMode();
removeErrors(ctx);
GemaBrowseControl browse = (GemaBrowseControl) ctx.session().getAttribute("outputs");
SelectedData data = browse.getAllSelectedData(ctx, true);
try {
bproxy.generateOutputs(data.getSelectedDboids(), form.getDboid(), false);
if (GemaAppErrorMgr.currentScopeHasErrors()) {
ctx.request().setAttribute(DetailRequest.MODE, mode);
showErrorPopUp(ctx);
} else {
String javascript = "<script>window.opener.refreshDivDocsRelated(); window.close();</script>";
ctx.response().getOutputStream().print(javascript);
ctx.forwardToResponse();
}
} catch (Exception e) {
handleException(e, ctx.request());
}
}
On my opinion it is extremely bad practice. Mixing the business logic and representation layers will not only make it harder to understand (what if the back-end dev does not understand JS at all?), but event more importantly it will make it harder to debug, especially in big projects where the people are constantly changing and this knowledge is lost in process.
I would implement an AJAX call and execute that JS upon request completion (on the view side ofc).
Related
I want to implement Google Play Games Services in my game on the libgdx engine. I tried using gdx-gamesvcs for this. But I am having trouble saving data. I understood from the example that one value is being saved, not the entire state of the game. So I decided to check it out: save and load one value using gsClient.loadGameState and gsClient.saveGameState. I deliberately deleted the game data from the device. But as a result, not only the test value changed, but many others as well. I thought that the state of the entire game is being saved, but the values obtained do not fit into the logic of the game and could not be obtained in it.
How should I use this tool and is it worth it at all, or is it better to use what libgdx itself offers?
Here is a piece of code:
if (gsClient.isSessionActive()) {
try {
gsClient.saveGameState("data", intToByteArray(testValue), 0, null);
} catch (UnsupportedOperationException unsupportedOperationException) {
}
if (gsClient.isSessionActive()) {
try {
gsClient.loadGameState("data", new ILoadGameStateResponseListener() {
#Override
public void gsGameStateLoaded(byte[] gameState) {
if (gameState != null) {
setTestValue(bytesToInt(gameState));
}
}
});
} catch (UnsupportedOperationException unsupportedOperationException) {
}
}
UPD
Yes, saving occurs both to the cloud and to the device, for saving to the device I use Preferences. I have a Google account login button in the game, it works, I have repeatedly seen this standard bar of my account level, which appears at the top when I log in. Everything is set up in the developer console too, I have an id for achievements and leaderboards. In code, I work with the client like this (In the create() method):
public IGameServiceClient gsClient;
if (gsClient == null) {
gsClient = new MockGameServiceClient(1) {
#Override
protected Array<ILeaderBoardEntry> getLeaderboardEntries() {
return null;
}
#Override
protected Array<String> getGameStates() {
return null;
}
#Override
protected byte[] getGameState() {
return new byte[0];
}
#Override
protected Array<IAchievement> getAchievements() {
return null;
}
#Override
protected String getPlayerName() {
return null;
}
};
}
gsClient.setListener(this);
gsClient.resumeSession();
Next is loading.
The exception is not caught, I removed it and everything works as before.
Well, libgdx offers no built-in cloud-save, it is hard to use it for that. :-)
You should in any case save to local AND to cloud, as the cloud is not very fast to load its state.
I can see no problem in your code besides the fact that you swallow an UnsupportedOperationException that is thrown if you did not activate cloud save feature. So the interesting question is: what happens if you don't swallow the exception, and did you intialize GpgsClient with cloud save enabled? Are you really logged in to Gpgs, and is the feature also activated in your developer console?
The main problem was that gameState was null, this arose due to the fact that you had to wait 24 hours after enabling the save function in the developer console, and the advice on clearing the memory of google play games on the test device did not help. After a while gameState began to pass the existing values, but I started having problems with the graphics flow, probably due to the asynchronous loading.
I am making an Android application and have decided to use Azure's DB platform. I find adding entries to different tables very easy, but querying the DB to be almost impossible.
Currently I am trying to follow this model: How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?
In short, it uses an interface to implement a AsyncTask with my query executing in doInBackground(). My problem with this is that it is many many lines of code to execute a single simple query, AND I will need to be doing multiple unique queries when running my app and creating tens of separate interfaces/classes seems extremely inefficient.
At the end of the day all I want is to get the results from this query in a managable way:
final MobileServiceList<Users> result = mUser.where().field("username").eq(username).execute().get();
However it seems to not execute without being wrapped in a AsyncTask like this:
new AsyncTask<Users, Void, Users>() {
#Override
protected Users doInBackground(Users... params) {
try {
final MobileServiceList<Users> result = mUser.where().field("username").eq(username).execute().get();
if(result.size() > 0) {
System.out.println("something in list");
return result.get(0);
}
} catch (Exception exception) {
exception.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Users result) {
System.out.println("in post");
if(username.equals((result.username)) && password.equals(result.password)) {
System.out.println("found user");
}
}
}.execute();
Is there any easy way to get around this? All I want is a result from a simple query, I don't understand how this is so hard..
Thanks!
Unfortunately, there is not any simple way to get around this, please refer to AsyncTask reference to know it for helping Android UI thread to get result asynchronously.
In an Android app that I'm writing, much of the app involves performing web requests to a specific API. Since the format of the data ends up being the same, I wanted to centralize many of the functions that I end up performing every request, rather than rewriting them every time.
For example, I perform the same error checking routine every time I make a web request:
JSONObject jo = new JSONObject(response);
boolean success = jo.getBoolean("success");
if(success) {
//Do work
} else {
//Display error
}
My thought was to make this some sort of class that implements Response.Listener, but I couldn't determine an effective way of handling errors and such. My question is is there an easy way to perform repeated functions in callbacks? I'm looking for an elegant solution, not necessarily the easiest.
You can achieve this by passing one or two interfaces to your method which is cumbersome. Java 8 brings you lambda which makes working with callback much more elegant. To use lambdas in Android you can use retrolambda: https://github.com/orfjackal/retrolambda
Your generic method could look like this
parseResponse(String response,
Consumer<JSONObject> successConsumer,
Consumer<String> errorConsumer) {
JSONObject jo = new JSONObject(response);
boolean success = jo.getBoolean("success");
if(success) {
successConsumer.accept(jo);
} else {
errorConsumer.accept("error");
}
}
You would use this method like this:
class MyClass {
void onResponse(String response) {
....
parseResponse(response, this::handleData, this::handleError);
}
void handleData(JSONObject object) {....}
void handleError(String object) {....}
}
I was recently reading about design patterns and especially about low coupling and delegation.
I was wondering, whether there should be any logic on the Activity class or if it only serves the view.
E.g. I have an activity called BattleActivity and that is supposed to work as some kind of session between two players. A lot of Push Notifications happen there, also the class works as an Observer, so there is a lot of comminication going on there.
Right now I am trying to figure out what logic could I move to a separated object(and whether I should) and then just work with the activity.
Example of one of my methods on the activity:
private void postCastedSpell(final int spellId) {
Call call = StaticGlobalContainer.api.postSpellToBattle(Integer.parseInt(battleId), Integer.parseInt(MainActivity.CURRENT_USER_ID), spellId, 100);
call.enqueue(new Callback<User>() {
#Override
public void onResponse(Response<User> response, Retrofit retrofit) {
User user = response.body();
if (response.code() == 202) {
// 200
Log.i("Posting spell to battle", "Success");
Boolean affectedUserIsOpponent = isUserOpponent(user);
if (affectedUserIsOpponent && user.currentHp<1){
StaticGlobalContainer.battleOnResult(Constants.WON, getApplicationContext());
}else {
updateBattleLog(affectedUserIsOpponent, user, spellId);
}
// TODO: do something here
} else {
// 404 or the response cannot be converted to User.
Log.e("Posting spell to battle", "Error:" + response.errorBody());
}
}
#Override
public void onFailure(Throwable t) {
Log.i("HttpRequest-Post spell", "Failure");
}
});
}
It's not specifically bad to put a lot of logic in Activities, but you're right to try to keep it only view related things. If the app is relatively small, it might not be worth moving the logic out. Also, there is some overhead to using abstractions.
if your abstractions aren't supplying a significant benefit, you should avoid them
I try to keep any big data objects in a manager class, so given your example, it might worthwhile to create a Battle manager class to hold all the logic involved in it, like this postCastedSpell function. This way all the Battle information is self contained, and also can be used elsewhere in other activities.
Just keep in mind if you're use data manager classes and you want them to prompt some sort of interation with the UI, you'll have to use Callbacks or the Bus pattern since the Battle manager won't have access to your UI. For example, to call the postCastedSpell the call would look like:
BattleActivity:
BattleManager bm = BattleManager.getInstance(user1, user2);
onSpellClicked() {
bm.castSpell(spellId, user1, callback)
}
BasicCallback callback = new BasicCallback() {
#Override
onComplete() {
if (MyInfoFragment.this.isVisible()) {
[Update UI]
}
}
};
NOTE: When using callbacks like my example, when it finally gets called the activity may have already gone out of view and have been already garbage collected. So in the callback function you need to first make sure it is still visible before trying to modify the UI that possibly no longer exists.
Every time my mobile app needs to access the DB, I have to write this piece of code.
//NetworkManager.getInstance().start();
ConnectionRequest request = new ConnectionRequest();
request.setUrl(WebConstants.HOST+"URL");
request.setPost(false);
request.addArgument("x",y);
request.addResponseListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
NetworkEvent event = (NetworkEvent)evt;
Exception error = event.getError();
if(error != null) {
return;
}
ConnectionRequest request = event.getConnectionRequest();
byte[] result = request.getResponseData();
String response = new String(result);
/** Process the string response **/
}
});
NetworkManager.getInstance().addToQueue(request);
This becomes really tedious when the web app constantly accesses the DB and for every call I need to copy, paste and modify same code. I tried to create a method and return the string response, but since the web service is done asynchronously, the method that calls the web service has to finish before web service method process the response. Therefore, I can't simplified this part of code. Any ideas on how to proceed?
Encapsulate the code in
/** Process the string response **/
into a Callable, then you can create a generic method that calls
callable.call()
You can use addToQueueAndWait(request) which blocks until the network operation completes and is completely safe to use on the EDT since it uses invoke and block.
You can also use something like the webservice wizard which encapsulates web calls in method calls.