[chronojump/77] session and server to 77



commit 2813219f89f88b8e5b9f80dd1c92355c4243edbb
Author: Xavier de Blas <xaviblas gmail com>
Date:   Thu Feb 25 18:57:23 2010 +0800

    session and server to 77

 chronojump_server/chronojumpServerCSharp.cs  |    6 +-
 src/Makefile.am                              |    3 +-
 src/oldCodeNeedToDBConvert/sqlite/session.cs |  111 ++++++++++++++++++++++++++
 src/server.cs                                |   75 ++++++++++--------
 src/sqlite/main.cs                           |   29 ++++++-
 src/sqlite/session.cs                        |   80 +------------------
 6 files changed, 187 insertions(+), 117 deletions(-)
---
diff --git a/chronojump_server/chronojumpServerCSharp.cs b/chronojump_server/chronojumpServerCSharp.cs
index 6bfc0cb..777c54c 100755
--- a/chronojump_server/chronojumpServerCSharp.cs
+++ b/chronojump_server/chronojumpServerCSharp.cs
@@ -291,11 +291,11 @@ public class ChronojumpServer {
 	}
 	
 	[WebMethod(Description="Upload person session if needed")]
-	public int UploadPersonSessionIfNeeded(int personServerID, int sessionServerID, double weight)
+	public int UploadPersonSessionIfNeeded(PersonSession ps)
 	{
-		if(!SqlitePersonSession.PersonSelectExistsInSession(personServerID, sessionServerID)) {
+		if(!SqlitePersonSession.PersonSelectExistsInSession(ps.PersonID, ps.SessionID)) {
 			Console.WriteLine("personSession needed");
-			SqlitePersonSession.Insert (false, Constants.PersonSessionWeightTable, "-1", personServerID, sessionServerID, weight);
+			ps.InsertAtDB(false, Constants.PersonSession);
 			Console.WriteLine("done");
 			return 1; //unused
 		} else 
diff --git a/src/Makefile.am b/src/Makefile.am
index f357ff6..65c35cb 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -184,7 +184,8 @@ FILES = \
  	oldCodeNeedToDBConvert/person.cs\
  	oldCodeNeedToDBConvert/personSession.cs\
  	oldCodeNeedToDBConvert/sqlite/person.cs\
- 	oldCodeNeedToDBConvert/sqlite/personSession.cs
+ 	oldCodeNeedToDBConvert/sqlite/personSession.cs\
+ 	oldCodeNeedToDBConvert/sqlite/session.cs
 
 CHRONOJUMP_MINI_FILES=\
 	chronojump_mini.cs\
diff --git a/src/oldCodeNeedToDBConvert/sqlite/session.cs b/src/oldCodeNeedToDBConvert/sqlite/session.cs
new file mode 100644
index 0000000..f3c3b18
--- /dev/null
+++ b/src/oldCodeNeedToDBConvert/sqlite/session.cs
@@ -0,0 +1,111 @@
+/*
+ * This file is part of ChronoJump
+ *
+ * ChronoJump is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation; either version 2 of the License, or   
+ *    (at your option) any later version.
+ *    
+ * ChronoJump is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
+ *    GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Copyright (C) 2004-2009   Xavier de Blas <xaviblas gmail com> 
+ */
+
+using System;
+using System.Data;
+using System.IO;
+using System.Collections; //ArrayList
+using Mono.Data.Sqlite;
+using Mono.Unix;
+
+
+class SqliteSessionOld : Sqlite
+{
+	public SqliteSessionOld() {
+	}
+	
+	~SqliteSessionOld() {}
+
+
+	/* OLD STUFF */
+	/* 
+	 * don't do more like this, use Sqlite.convertTables()
+	 */
+	//change DB from 0.55 to 0.56
+	protected internal static void convertTableAddingSportStuff() 
+	{
+		ArrayList myArray = new ArrayList(2);
+
+		//1st create a temp table
+		//createTable(Constants.ConvertTempTable);
+		SqliteSession sqliteSessionObject = new SqliteSession();
+		sqliteSessionObject.createTable(Constants.ConvertTempTable);
+			
+		//2nd copy all data from session table to temp table
+		dbcmd.CommandText = "SELECT * " + 
+			"FROM " + Constants.SessionTable + " ORDER BY uniqueID"; 
+		SqliteDataReader reader;
+		reader = dbcmd.ExecuteReader();
+		while(reader.Read()) {
+			Session mySession = new Session(reader[0].ToString(), reader[1].ToString(), 
+					reader[2].ToString(), UtilDate.FromSql(reader[3].ToString()), 
+					1, //sport undefined
+					-1, //speciallity undefined
+					-1, //practice level undefined
+					reader[4].ToString(), //comments
+					Constants.ServerUndefinedID
+					); 
+			myArray.Add(mySession);
+		}
+		reader.Close();
+
+		foreach (Session mySession in myArray)
+			InsertOld(true, Constants.ConvertTempTable,
+				mySession.Name, mySession.Place, UtilDate.ToSql(mySession.Date), 
+				mySession.PersonsSportID, mySession.PersonsSpeciallityID, mySession.PersonsPractice, mySession.Comments);
+
+		//3rd drop table sessions
+		Sqlite.dropTable(Constants.SessionTable);
+
+		//4d create table persons (now with sport related stuff
+		//createTable(Constants.SessionTable);
+		sqliteSessionObject.createTable(Constants.SessionTable);
+
+		//5th insert data in sessions (with sport related stuff)
+		foreach (Session mySession in myArray) 
+			InsertOld(true, Constants.SessionTable,
+				mySession.Name, mySession.Place, UtilDate.ToSql(mySession.Date), 
+				mySession.PersonsSportID, mySession.PersonsSpeciallityID, mySession.PersonsPractice, mySession.Comments);
+
+
+		//6th drop temp table
+		Sqlite.dropTable(Constants.ConvertTempTable);
+	}
+	
+	/* used only on conversion from 0.55 to 0.56 */
+	public static int InsertOld(bool dbconOpened, string tableName, string name, string place, string date, int personsSportID, int personsSpeciallityID, int personsPractice, string comments)
+	{
+		if(! dbconOpened)
+			dbcon.Open();
+
+		dbcmd.CommandText = "INSERT INTO " + tableName + " (uniqueID, name, place, date, personsSportID, personsSpeciallityID, personsPractice, comments)" +
+			" VALUES (NULL, '"
+			+ name + "', '" + place + "', '" + date + "', " + 
+			personsSportID + ", " + personsSpeciallityID + ", " + 
+			personsPractice + ", '" + comments + "')" ;
+		dbcmd.ExecuteNonQuery();
+		int myReturn = dbcon.LastInsertRowId;
+		
+		if(! dbconOpened)
+			dbcon.Close();
+
+		return myReturn;
+	}
+}
diff --git a/src/server.cs b/src/server.cs
index b97540b..d958575 100644
--- a/src/server.cs
+++ b/src/server.cs
@@ -188,7 +188,8 @@ public class Server
 			string sportsSeparator = "";
 
 			//upload persons (updating also person.serverUniqueID locally)
-			string [] myPersons = SqlitePersonSession.SelectCurrentSession(serverSession.UniqueID, true, false); //onlyIDAndName, not reversed
+			//string [] myPersons = SqlitePersonSession.SelectCurrentSession(serverSession.UniqueID, true, false); //onlyIDAndName, not reversed
+			ArrayList persons = SqlitePersonSession.SelectCurrentSessionPersons(serverSession.UniqueID); 
 			
 			Constants.UploadCodes uCode;
 			ArrayList notToUpload = SqlitePersonSessionNotUpload.SelectAll(currentSession.UniqueID);
@@ -196,45 +197,50 @@ public class Server
 			//store in variable for updating progressBar from other thread
 			progressBarPersonsNum = myPersons.Length - notToUpload.Count;
 
-			foreach(string personStr in myPersons) {
-				Person person = SqlitePersonSession.PersonSelect(Util.FetchID(personStr), serverSession.UniqueID); 
+			//foreach(string personStr in myPersons) {
+			//	Person person = SqlitePersonSession.PersonSelect(Util.FetchID(personStr), serverSession.UniqueID); 
+			foreach(Person person in persons) {
 
 				//do not continue with this person if has been banned to upload
 				if(Util.FoundInArrayList(notToUpload, person.UniqueID.ToString()))
 					continue;
 
+				PersonSession ps = SqlitePersonSession.Select(person.UniqueID, currentSession.UniqueID);  
+
 				//check person if exists
 				if(person.ServerUniqueID != Constants.ServerUndefinedID) 
 					uCode = Constants.UploadCodes.EXISTS;
 				else {
 					uCode = Constants.UploadCodes.OK;
 
-					//if sport is user defined, upload it
-					//and when upload the person, do it with new sportID
-					Sport sport = SqliteSport.Select(person.SportID);
-					//but record old sport ID because locally will be a change in serverUniqueID
-					//(with slite update)
-					//but local sport has not to be changed
-					int sportUserDefinedLocal = -1;
-
-					if(sport.UserDefined) {
-						sportUserDefinedLocal = sport.UniqueID;
-
-						//this will be uploaded
-						int newSport = myServer.UploadSport(sport);
-						if(newSport != -1) {
-							person.SportID = newSport;
-							sessionUploadPersonData.sports += sportsSeparator + sport.Name;
-							sportsSeparator = ", ";
-						}
+					//person = serverUploadPerson(myServer, person, serverSession.UniqueID, sportUserDefinedLocal);
+					person = serverUploadPerson(myServer, person, serverSession.UniqueID);
+				}
+					
+				//if sport is user defined, upload it
+				//and when upload the person, do it with new sportID
+				Sport sport = SqliteSport.Select(ps.SportID);
+				//but record old sport ID because locally will be a change in serverUniqueID
+				//(with slite update)
+				//but local sport has not to be changed
+				int sportUserDefinedLocal = -1;
+
+				if(sport.UserDefined) {
+					sportUserDefinedLocal = sport.UniqueID;
+
+					//this will be uploaded
+					int newSport = myServer.UploadSport(sport);
+					if(newSport != -1) {
+						ps.SportID = newSport;
+						sessionUploadPersonData.sports += sportsSeparator + sport.Name;
+						sportsSeparator = ", ";
 					}
-
-					person = serverUploadPerson(myServer, person, serverSession.UniqueID, sportUserDefinedLocal);
 				}
 
 				//a person can be in the database for one session, 
-				//but maybe now we add jumps from another session and we should add an entry at personsessionweight
-				serverUploadPersonSessionIfNeeded(myServer, person.ServerUniqueID, currentSession.ServerUniqueID, person.Weight);
+				//but maybe now we add jumps from another session and we should add an entry at personsession
+				serverUploadPersonSessionIfNeeded(myServer, person.ServerUniqueID, 
+						currentSession.ServerUniqueID, ps, sportUserDefinedLocal);
 
 				//other thread updates the gui:
 				sessionUploadPersonData.person = person;
@@ -539,25 +545,30 @@ public class Server
 	
 	
 	//upload a person
-	private static Person serverUploadPerson(ChronojumpServer myServer, Person person, int serverSessionID, int sportUserDefinedLocal) 
+	//private static Person serverUploadPerson(ChronojumpServer myServer, Person person, int serverSessionID, int sportUserDefinedLocal) 
+	private static Person serverUploadPerson(ChronojumpServer myServer, Person person, int serverSessionID) 
 	{
 		int idAtServer = myServer.UploadPerson(person, serverSessionID);
 
 		//update person (serverUniqueID) on client database
 		person.ServerUniqueID = idAtServer;
 
-		//when update locally, don't put the user defined sport id at server
-		if(sportUserDefinedLocal != -1)
-			person.SportID = sportUserDefinedLocal;
-
 		SqlitePerson.Update(person);
 
 		return person;
 	}
 
-	private static void serverUploadPersonSessionIfNeeded(ChronojumpServer myServer, int personServerID, int sessionServerID, double weight)
+	//private static void serverUploadPersonSessionIfNeeded(ChronojumpServer myServer, int personServerID, int sessionServerID, double weight)
+	private static void serverUploadPersonSessionIfNeeded(ChronojumpServer myServer, 
+			int personServerID, int sessionServerID, PersonSession ps, int sportUserDefinedLocal)
 	{
-		myServer.UploadPersonSessionIfNeeded(personServerID, sessionServerID, weight);
+		//when update locally, don't put the user defined sport id at server
+		if(sportUserDefinedLocal != -1)
+			ps.SportID = sportUserDefinedLocal;
+
+		ps.PersonID = personServerID;
+		ps.ServerID = sessionServerID;
+		myServer.UploadPersonSessionIfNeeded(ps);
 	}
 
 	//upload a test
diff --git a/src/sqlite/main.cs b/src/sqlite/main.cs
index 65513c6..b09ccfc 100644
--- a/src/sqlite/main.cs
+++ b/src/sqlite/main.cs
@@ -665,7 +665,7 @@ class Sqlite
 			if(currentVersion == "0.55") {
 				dbcon.Open();
 
-				SqliteSession.convertTableAddingSportStuff();
+				SqliteSessionOld.convertTableAddingSportStuff();
 
 				SqlitePreferences.Update ("databaseVersion", "0.56", true); 
 				dbcon.Close();
@@ -955,7 +955,7 @@ class Sqlite
 			if(currentVersion == "0.72") {
 				dbcon.Open();
 				
-				deleteOrphanedPersons();
+				deleteOrphanedPersonsOld();
 
 				SqlitePreferences.Update ("databaseVersion", "0.73", true); 
 				
@@ -1346,9 +1346,32 @@ class Sqlite
 	}
 
 	//used to delete persons (if needed) when a session is deleted. See SqliteSession.DeleteAllStuff
-	//also used to convert to sqlite 0.73
 	protected internal static void deleteOrphanedPersons()
 	{
+		dbcmd.CommandText = "SELECT uniqueID FROM " + Constants.PersonTable;
+		Log.WriteLine(dbcmd.CommandText.ToString());
+		dbcmd.ExecuteNonQuery();
+		
+		SqliteDataReader reader;
+		reader = dbcmd.ExecuteReader();
+		ArrayList myArray = new ArrayList(1);
+
+		while(reader.Read())
+			myArray.Add (Convert.ToInt32(reader[0]));
+		reader.Close();
+
+		foreach(int personID in myArray) {
+			//if person is not in other sessions, delete it from DB
+			if(! SqlitePersonSession.PersonExistsInPSW(personID))
+				SqlitePerson.Delete(personID);
+		}
+	}
+				
+	//used to delete persons (if needed) when a session is deleted. See SqliteSession.DeleteAllStuff
+	//also used to convert to sqlite 0.73
+	//this is old method (before .77), now use above method
+	protected internal static void deleteOrphanedPersonsOld()
+	{
 		dbcmd.CommandText = "SELECT uniqueID FROM " + Constants.PersonOldTable;
 		Log.WriteLine(dbcmd.CommandText.ToString());
 		dbcmd.ExecuteNonQuery();
diff --git a/src/sqlite/session.cs b/src/sqlite/session.cs
index e3d04f2..fabf51f 100644
--- a/src/sqlite/session.cs
+++ b/src/sqlite/session.cs
@@ -247,7 +247,7 @@ class SqliteSession : Sqlite
 		 * */
 		
 		//select persons of each session
-		dbcmd.CommandText = "SELECT sessionID, count(*) FROM " + Constants.PersonSessionWeightTable + 
+		dbcmd.CommandText = "SELECT sessionID, count(*) FROM " + Constants.PersonSessionTable + 
 			" GROUP BY sessionID ORDER BY sessionID";
 		Log.WriteLine(dbcmd.CommandText.ToString());
 		dbcmd.ExecuteNonQuery();
@@ -527,7 +527,7 @@ class SqliteSession : Sqlite
 		dbcmd.ExecuteNonQuery();
 		
 		//delete relations (existance) within persons and sessions in this session
-		dbcmd.CommandText = "Delete FROM " + Constants.PersonSessionWeightTable + " WHERE sessionID == " + uniqueID;
+		dbcmd.CommandText = "Delete FROM " + Constants.PersonSessionTable + " WHERE sessionID == " + uniqueID;
 		dbcmd.ExecuteNonQuery();
 
 		Sqlite.deleteOrphanedPersons();
@@ -564,82 +564,6 @@ class SqliteSession : Sqlite
 		dbcon.Close();
 	}
 
-
-	/* OLD STUFF */
-
-	/* 
-	 * don't do more like this, use Sqlite.convertTables()
-	 */
-	//change DB from 0.55 to 0.56
-	protected internal static void convertTableAddingSportStuff() 
-	{
-		ArrayList myArray = new ArrayList(2);
-
-		//1st create a temp table
-		//createTable(Constants.ConvertTempTable);
-		SqliteSession sqliteSessionObject = new SqliteSession();
-		sqliteSessionObject.createTable(Constants.ConvertTempTable);
-			
-		//2nd copy all data from session table to temp table
-		dbcmd.CommandText = "SELECT * " + 
-			"FROM " + Constants.SessionTable + " ORDER BY uniqueID"; 
-		SqliteDataReader reader;
-		reader = dbcmd.ExecuteReader();
-		while(reader.Read()) {
-			Session mySession = new Session(reader[0].ToString(), reader[1].ToString(), 
-					reader[2].ToString(), UtilDate.FromSql(reader[3].ToString()), 
-					1, //sport undefined
-					-1, //speciallity undefined
-					-1, //practice level undefined
-					reader[4].ToString(), //comments
-					Constants.ServerUndefinedID
-					); 
-			myArray.Add(mySession);
-		}
-		reader.Close();
-
-		foreach (Session mySession in myArray)
-			InsertOld(true, Constants.ConvertTempTable,
-				mySession.Name, mySession.Place, UtilDate.ToSql(mySession.Date), 
-				mySession.PersonsSportID, mySession.PersonsSpeciallityID, mySession.PersonsPractice, mySession.Comments);
-
-		//3rd drop table sessions
-		Sqlite.dropTable(Constants.SessionTable);
-
-		//4d create table persons (now with sport related stuff
-		//createTable(Constants.SessionTable);
-		sqliteSessionObject.createTable(Constants.SessionTable);
-
-		//5th insert data in sessions (with sport related stuff)
-		foreach (Session mySession in myArray) 
-			InsertOld(true, Constants.SessionTable,
-				mySession.Name, mySession.Place, UtilDate.ToSql(mySession.Date), 
-				mySession.PersonsSportID, mySession.PersonsSpeciallityID, mySession.PersonsPractice, mySession.Comments);
-
-
-		//6th drop temp table
-		Sqlite.dropTable(Constants.ConvertTempTable);
-	}
-	
-	/* used only on conversion from 0.55 to 0.56 */
-	public static int InsertOld(bool dbconOpened, string tableName, string name, string place, string date, int personsSportID, int personsSpeciallityID, int personsPractice, string comments)
-	{
-		if(! dbconOpened)
-			dbcon.Open();
-
-		dbcmd.CommandText = "INSERT INTO " + tableName + " (uniqueID, name, place, date, personsSportID, personsSpeciallityID, personsPractice, comments)" +
-			" VALUES (NULL, '"
-			+ name + "', '" + place + "', '" + date + "', " + 
-			personsSportID + ", " + personsSpeciallityID + ", " + 
-			personsPractice + ", '" + comments + "')" ;
-		dbcmd.ExecuteNonQuery();
-		int myReturn = dbcon.LastInsertRowId;
-		
-		if(! dbconOpened)
-			dbcon.Close();
-
-		return myReturn;
-	}
 }
 
 class SqliteServerSession : SqliteSession



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]