ORCentral
|
00001 package eu.coform.command.loginsession; 00002 00003 import eu.coform.*; 00004 import eu.coform.Error; 00005 import eu.coform.database.*; 00006 import eu.coform.database.command.CheckLocationDBCommand; 00007 import eu.coform.database.command.CheckLocationSessionExistsDBCommand; 00008 import eu.coform.database.command.CheckLoginDBCommand; 00009 import eu.coform.database.command.CheckPrimarySessionDBCommand; 00010 import eu.coform.database.command.GetNewSessionUIDDBCommand; 00011 00012 import eu.coform.database.command.NewSessionDBCommand; 00013 import eu.coform.database.command.UpdateExistingSessionDBCommand; 00014 00015 00016 00020 public class LoginCommand implements AbstractSectionLoginSession{ 00021 00022 private Location location; 00023 private String username; 00024 private String password; 00025 private int minutesTimeout; 00026 00038 @Override 00039 public Session execute() throws ORException { 00040 boolean primary; 00041 00042 // check login data 00043 00044 CheckLoginDBCommand checklogin = new CheckLoginDBCommand(username); 00045 00046 try { 00047 checklogin.exec(); 00048 } catch (DBException e1) { 00049 if(e1.error() == eu.coform.database.DBException.Error.ObjectNotFound) 00050 { 00051 // no user with this username 00052 throw new ORException(Error.UsernamePwWrong); 00053 } 00054 else 00055 { 00056 throw new ORException(Error.InternalError); 00057 } 00058 } 00059 00060 if(! password.equals(checklogin.getPasswordResult())) 00061 { 00062 // wrong password 00063 throw new ORException(Error.UsernamePwWrong); 00064 } 00065 00066 00067 long userId = checklogin.getUserIdResult(); 00068 00069 00070 //check if location is existent 00071 00072 CheckLocationDBCommand locCorrect = new CheckLocationDBCommand(location); 00073 00074 try { 00075 locCorrect.exec(); 00076 } catch (DBException e1) { 00077 // TODO Auto-generated catch block 00078 throw new ORException(Error.LocationInvalid); 00079 } 00080 00081 long locationId = locCorrect.getResult(); 00082 00083 if(locationId == 0) 00084 { 00085 throw new ORException(Error.LocationInvalid); 00086 } 00087 00088 00089 00090 00091 00092 //ExpirationDate 00093 long expiration_date = System.currentTimeMillis() / 1000L;; // m_minutesTimout; 00094 00095 // get a unique id and check if it is really unique 00096 UniqueID uuid = null; 00097 00098 GetNewSessionUIDDBCommand newUuid = new GetNewSessionUIDDBCommand(); 00099 00100 try { 00101 newUuid.exec(); 00102 } catch (DBException e1) { 00103 throw new ORException(Error.InternalError); 00104 } 00105 00106 uuid = newUuid.getResult(); 00107 00108 00109 // check if user has already a session 00110 // TODO: wenn session gelšscht wird muss gecheckt wird ob neue session daten bekommt 00111 00112 00113 00114 CheckPrimarySessionDBCommand isPrim = new CheckPrimarySessionDBCommand(userId); 00115 00116 try { 00117 isPrim.exec(); 00118 } catch (DBException e) { 00119 throw new ORException(Error.InternalError); 00120 } 00121 00122 primary = isPrim.getResult(); 00123 00124 00125 if(!primary) 00126 { 00127 // is there already a session for this user and location 00128 CheckLocationSessionExistsDBCommand sessionEx = new CheckLocationSessionExistsDBCommand(userId, locationId); 00129 try { 00130 sessionEx.exec(); 00131 } catch (DBException e) { 00132 00133 throw new ORException(Error.InternalError); 00134 00135 } 00136 00137 if(sessionEx.getResult()) 00138 { 00139 // exists already 00140 UpdateExistingSessionDBCommand upd = new UpdateExistingSessionDBCommand(uuid, expiration_date, minutesTimeout, userId, locationId); 00141 try { 00142 upd.exec(); 00143 } catch (DBException e) { 00144 00145 throw new ORException(Error.InternalError); 00146 00147 } 00148 00149 Session newsession = new Session(); 00150 newsession.setSessionID(uuid); 00151 00152 return newsession; 00153 } 00154 00155 } 00156 00157 00158 //create new Session in db 00159 NewSessionDBCommand newSession = new NewSessionDBCommand(uuid, expiration_date, minutesTimeout, userId, locationId, primary); 00160 00161 try { 00162 newSession.exec(); 00163 } catch (DBException e) { 00164 throw new ORException(Error.InternalError); 00165 } 00166 00167 Session newsession = new Session(); 00168 newsession.setSessionID(uuid); 00169 00170 return newsession; 00171 00172 } 00173 00174 00175 public void setLocation(Location m_location) { 00176 this.location = m_location; 00177 } 00178 00179 public Location getLocation() { 00180 return location; 00181 } 00182 00183 public void setUsername(String m_username) { 00184 this.username = m_username; 00185 } 00186 00187 public String getUsername() { 00188 return username; 00189 } 00190 00191 public void setMinutesTimout(int minutesTimout) { 00192 this.minutesTimeout = minutesTimout; 00193 } 00194 00195 public int getMinutesTimout() { 00196 return minutesTimeout; 00197 } 00198 00199 public void setPassword(String password) { 00200 this.password = password; 00201 } 00202 00203 public String getPassword() { 00204 return password; 00205 } 00206 00207 }