Skip to content

Commit

Permalink
ORA-651 add a clear REST-call, fix unit test error
Browse files Browse the repository at this point in the history
  • Loading branch information
rbudde committed Sep 18, 2015
1 parent 74e8ddb commit 191757d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import de.fhg.iais.roberta.persistence.util.DbSession;
import de.fhg.iais.roberta.persistence.util.HttpSessionState;
import de.fhg.iais.roberta.util.Key;
import de.fhg.iais.roberta.util.Pair;
import de.fhg.iais.roberta.util.Util;

public class ProgramProcessor extends AbstractProcessor {
Expand All @@ -24,8 +25,8 @@ public ProgramProcessor(DbSession dbSession, HttpSessionState httpSessionState)
}

/**
* load a program from the data base. Either the program is owned by the user with the id given or the program is shared
* by the user given to the user requesting the program
* load a program from the data base. Either the program is owned by the user with the id given or the program is shared by the user given to the user
* requesting the program
*
* @param programName the program to load
* @param ownerId the owner (either the user logged in or an owner who shared the program R/W with the user logged in
Expand Down Expand Up @@ -173,26 +174,28 @@ private Program getProgramWithAccessRight(String programName, int ownerId) {
* @param mayExist true, if an existing program may be changed; false if a program may be stored only, if it does not exist in the database
* @param isOwner true, if the owner updates a program; false if a user with access right WRITE updates a program
*/
public void updateProgram(String programName, int userId, int robotId, String programText, Timestamp programTimestamp, boolean isOwner) {
public Program updateProgram(String programName, int userId, int robotId, String programText, Timestamp programTimestamp, boolean isOwner) {
if ( !Util.isValidJavaIdentifier(programName) ) {
setError(Key.PROGRAM_ERROR_ID_INVALID, programName);
return;
return null;
}
if ( this.httpSessionState.isUserLoggedIn() ) {
UserDao userDao = new UserDao(this.dbSession);
RobotDao robotDao = new RobotDao(this.dbSession);
ProgramDao programDao = new ProgramDao(this.dbSession);
User user = userDao.get(userId);
Robot robot = robotDao.get(robotId);
Key result = programDao.persistProgramText(programName, user, robot, programText, programTimestamp, isOwner);
Pair<Key, Program> result = programDao.persistProgramText(programName, user, robot, programText, programTimestamp, isOwner);
// a bit strange, but necessary to distinguish between success and failure
if ( result == Key.PROGRAM_SAVE_SUCCESS ) {
if ( result.getFirst() == Key.PROGRAM_SAVE_SUCCESS ) {
setSuccess(Key.PROGRAM_SAVE_SUCCESS);
} else {
setError(result);
setError(result.getFirst());
}
return result.getSecond();
} else {
setError(Key.USER_ERROR_NOT_LOGGED_IN);
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import de.fhg.iais.roberta.persistence.bo.User;
import de.fhg.iais.roberta.persistence.util.DbSession;
import de.fhg.iais.roberta.util.Key;
import de.fhg.iais.roberta.util.Pair;
import de.fhg.iais.roberta.util.dbc.Assert;

/**
Expand Down Expand Up @@ -46,7 +47,7 @@ public ProgramDao(DbSession session) {
* @param isOwner true, if the owner updates a program; false if a user with access right WRITE updates a program
* @return true if the program could be persisted successfully
*/
public Key persistProgramText(String name, User user, Robot robot, String programText, Timestamp timestamp, boolean isOwner) {
public Pair<Key, Program> persistProgramText(String name, User user, Robot robot, String programText, Timestamp timestamp, boolean isOwner) {
Assert.notNull(name);
Assert.notNull(user);
Assert.notNull(robot);
Expand All @@ -60,30 +61,30 @@ public Key persistProgramText(String name, User user, Robot robot, String progra
program = new Program(name, user, robot);
program.setProgramText(programText);
this.session.save(program);
return Key.PROGRAM_SAVE_SUCCESS; // the only legal key if success
return Pair.of(Key.PROGRAM_SAVE_SUCCESS, program); // the only legal key if success
} else {
// otherwise ...
ProgramDao.LOG.error("update was requested, but no program with matching timestamp was found");
return Key.PROGRAM_SAVE_ERROR_NO_PROGRAM_TO_UPDATE_FOUND;
return Pair.of(Key.PROGRAM_SAVE_ERROR_NO_PROGRAM_TO_UPDATE_FOUND, null);
}
} else if ( !timestamp.equals(program.getLastChanged()) ) {
ProgramDao.LOG.error("update was requested, timestamps don't match. Has another user updated the program in the meantime?");
return Key.PROGRAM_SAVE_ERROR_OPTIMISTIC_TIMESTAMP_LOCKING;
return Pair.of(Key.PROGRAM_SAVE_ERROR_OPTIMISTIC_TIMESTAMP_LOCKING, null);
} else {
program.setProgramText(programText);
return Key.PROGRAM_SAVE_SUCCESS; // the only legal key if success
return Pair.of(Key.PROGRAM_SAVE_SUCCESS, program); // the only legal key if success
}
} else {
Program program = loadSharedForUpdate(name, user, robot);
if ( program == null ) {
ProgramDao.LOG.error("update was requested, but no shared program was found");
return Key.PROGRAM_SAVE_ERROR_NO_WRITE_PERMISSION;
return Pair.of(Key.PROGRAM_SAVE_ERROR_NO_WRITE_PERMISSION, null);
} else if ( !timestamp.equals(program.getLastChanged()) ) {
ProgramDao.LOG.error("update was requested, timestamps don't match. Has another user updated the program in the meantime?");
return Key.PROGRAM_SAVE_ERROR_OPTIMISTIC_TIMESTAMP_LOCKING;
return Pair.of(Key.PROGRAM_SAVE_ERROR_OPTIMISTIC_TIMESTAMP_LOCKING, null);
} else {
program.setProgramText(programText);
return Key.PROGRAM_SAVE_SUCCESS; // the only legal key if success
return Pair.of(Key.PROGRAM_SAVE_SUCCESS, program); // the only legal key if success
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import de.fhg.iais.roberta.util.dbc.Assert;

public class HttpSessionState {
private int userId = -1;
public final static int NO_USER = -1;

private int userId = HttpSessionState.NO_USER;
private int robotId = 42;
private String token = "1Q2W3E4R";
private String programName;
Expand All @@ -28,20 +30,11 @@ public boolean isUserLoggedIn() {
return this.userId >= 1;
}

public void rememberLogin(int userId) {
Assert.isTrue(userId >= 1);
public void setUserClearDataKeepTokenAndRobotId(int userId) {
Assert.isTrue(userId >= 1 || userId == HttpSessionState.NO_USER);
// token is not cleared. This would annoy the user.
// robotId is not cleared. This would annoy the user.
this.userId = userId;
// robotId is not cleared.
this.programName = null;
this.program = null;
this.configurationName = null;
this.configuration = null;
}

public void rememberLogout() {
this.userId = -1;
// token is not cleared. This would annoy the user.
this.programName = null;
this.program = null;
this.configurationName = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,12 @@ public Response command(@OraData HttpSessionState httpSessionState, JSONObject f
Long timestamp = request.getLong("timestamp");
Timestamp programTimestamp = new Timestamp(timestamp);
boolean isShared = request.optBoolean("shared", false);
pp.updateProgram(programName, userId, robotId, programText, programTimestamp, !isShared);
Program program = pp.updateProgram(programName, userId, robotId, programText, programTimestamp, !isShared);
if ( pp.isOk() ) {
Program program = pp.getProgram(programName, userId, robotId);
if ( program != null ) {
response.put("lastChanged", program.getLastChanged());
} else {
ClientProgram.LOG.error("TODO: check potential error: the saved program should never be null");
}
}
Util.addResultInfo(response, pp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import com.google.inject.Inject;

import de.fhg.iais.roberta.javaServer.provider.OraData;
import de.fhg.iais.roberta.persistence.AccessRightProcessor;
import de.fhg.iais.roberta.persistence.UserProcessor;
import de.fhg.iais.roberta.persistence.bo.User;
import de.fhg.iais.roberta.persistence.util.DbSession;
Expand Down Expand Up @@ -51,17 +50,23 @@ public Response command(@OraData HttpSessionState httpSessionState, @OraData DbS
ClientUser.LOG.info("command is: " + cmd);
response.put("cmd", cmd);
UserProcessor up = new UserProcessor(dbSession, httpSessionState);
AccessRightProcessor upp = new AccessRightProcessor(dbSession, httpSessionState);

if ( cmd.equals("login") && !httpSessionState.isUserLoggedIn() ) {
if ( cmd.equals("clear") ) {
httpSessionState.setUserClearDataKeepTokenAndRobotId(HttpSessionState.NO_USER);
response.put("rc", "ok");
if ( userId != HttpSessionState.NO_USER ) {
ClientUser.LOG.info("clear for (logged in) user " + userId + ". Has the user reloaded the page?");
}

} else if ( cmd.equals("login") && !httpSessionState.isUserLoggedIn() ) {
String userAccountName = request.getString("accountName");
String password = request.getString("password");
User user = up.getUser(userAccountName, password);
Util.addResultInfo(response, up);
if ( user != null ) {
int id = user.getId();
String account = user.getAccount();
httpSessionState.rememberLogin(id);
httpSessionState.setUserClearDataKeepTokenAndRobotId(id);
user.setLastLogin();
response.put("userId", id);
response.put("userRole", user.getRole());
Expand All @@ -71,7 +76,7 @@ public Response command(@OraData HttpSessionState httpSessionState, @OraData DbS
}

} else if ( cmd.equals("logout") && httpSessionState.isUserLoggedIn() ) {
httpSessionState.rememberLogout();
httpSessionState.setUserClearDataKeepTokenAndRobotId(HttpSessionState.NO_USER);
response.put("rc", "ok");
response.put("message", Key.USER_LOGOUT_SUCCESS.getKey());
ClientUser.LOG.info("logout of user " + userId);
Expand Down

0 comments on commit 191757d

Please sign in to comment.