Skip to content

Commit

Permalink
cambiato metodo di modifica dei dati di Work e Session
Browse files Browse the repository at this point in the history
ora viene eseguito un controllo hash per controllare che non si creino
duplicati.
  • Loading branch information
Davide Valsecchi committed Jul 15, 2012
1 parent 7272d26 commit f92a4f5
Show file tree
Hide file tree
Showing 8 changed files with 1,316 additions and 518 deletions.
1 change: 1 addition & 0 deletions QuickAgenda/QuickAgenda_settings.xml
Expand Up @@ -6,5 +6,6 @@
<path>C:\Users\Davide\Desktop\prova3.qad</path>
<path>C:\Users\Davide\Desktop\prova4.qad</path>
<path>C:\Users\Davide\Desktop\QuickAgenda\prova4.qad</path>
<path>C:\Users\Davide\Desktop\QuickAgenda\prova_finale.qad</path>
</data_path>
</settings>
60 changes: 44 additions & 16 deletions QuickAgenda/src/it/valsecchi/quickagenda/data/DataManager.java
Expand Up @@ -1103,30 +1103,58 @@ public void removeWork(String ID) throws IDNotFoundException {
}

/**
* Metodo che cambia il Costumer a cui è riferito un Work. Per far questo
* bisogna anche cambiare il CostumerID anche di tutte le session del Work.
* Metodo di collegamento al metodo di modifica dei Work del WorkManager.
* Inoltre il metodo aggiorna tutte le sessioni al corretto costumerID del
* Work.
*
* @param workid
* id del work a cui cambiare il costumerID
* @param newCostumerid
* ID del nuovo costumer del Work
* @param workID
* @param nome
* @param indirizzo
* @param costumerid
* @param iniziolavori
* @param finelavori
* @param completed
* @throws IDNotFoundException
* lanciato nel caso non si trovi il Work o il Costumer
* @throws WorkAlreadyExistsException
*/
public void changeWorkCostumerID(String workID, String newCostumerID)
throws IDNotFoundException {
if (!costumersMan.exists(newCostumerID)) {
throw new IDNotFoundException(ElementType.Costumer, newCostumerID);
public void modifyWork(String workID, String nome, String indirizzo,
String costumerid, Calendar iniziolavori, Calendar finelavori,
boolean completed) throws IDNotFoundException,
WorkAlreadyExistsException {
// si controlla prima di tutto che esista il costumerid
if (!costumersMan.exists(costumerid)) {
throw new IDNotFoundException(ElementType.Costumer, costumerid);
}
// si cambia l'id al work
worksMan.getWorkByID(workID).setCostumerID(newCostumerID);
// si modifica il Work
try {
worksMan.modifyWork(workID, nome, indirizzo, costumerid,
iniziolavori, finelavori, completed);
} catch (WorkAlreadyExistsException e) {
Log.error("impossibile cambiare dati, Work esiste già.");
throw e;
}
// ora bisogna aggiornare tutte le sessioni al corretto costumerid
// si ricavano le sessioni
List<Session> sessions = sessionsMan.queryByWorkID(workID);
for (Session s : sessions) {
s.setCostumerID(newCostumerID);
s.setCostumerID(costumerid);
}
// si lancia un aggiornamento delle session. l'aggiornamento dei Work
// verrà lanciato dal codice chiamante
// si lancia un aggiornamento delle Session
this.fireDataUpdatePerformed(ElementType.Session);
// si lancia l'aggiornamento dei Work
this.fireDataUpdatePerformed(ElementType.Work);
}

/**
* Metodo di collegamento al metodo di modifica delle Session del
* SessionManager
*/
public void modifySession(String sessionID, Calendar sessiondata,
int hours, int spesa, String note)
throws SessionAlreadyExistsException, IDNotFoundException {
// si modifica la sessione
sessionsMan.modifySession(sessionID, sessiondata, hours, spesa, note);
// si lancia l'aggiornamento
this.fireDataUpdatePerformed(ElementType.Session);
}
}
Expand Up @@ -69,6 +69,15 @@ public static String calculateSessionHash(String workid, String costumerid,
Calendar sessiondata2) {
return Utility.getHash(workid + costumerid + sessiondata2.toString());
}

/**
* Metodo che ricalcola l'hash della Session. La assegna alla Session e la restituisce.
* @return restituisce la nuova hash della Session
*/
public String recalculateSessionHash(){
this.hash = Utility.getHash(this.workID+this.costumerID+this.getSessionDataString());
return this.hash;
}

public Calendar getSessionData() {
return sessionData;
Expand Down
Expand Up @@ -287,6 +287,48 @@ public String getValidID() {
return id;
}

/**
* Metodo che modifica i dati di una Session impostando la nuova hash ed
* inserendola nella lista {@link #hashMap}.
*
* @param sessionID
* @param sessiondata
* @param hours
* @param spesa
* @param note
* @throws IDNotFoundException
* @throws SessionAlreadyExistsException
*/
public void modifySession(String sessionID, Calendar sessiondata,
int hours, int spesa, String note) throws IDNotFoundException,
SessionAlreadyExistsException {
// si ricava la sessione
if (sessionsMap.containsKey(sessionID)) {
Session s = sessionsMap.get(sessionID);
String vecchiaHash = s.getHash();
// viene calcolata la nuova hash
String newHash = Session.calculateSessionHash(s.getWorkID(),
s.getCostumerID(), sessiondata);
// si controlla che non esisti già
if (hashMap.containsKey(newHash)) {
// si lancia l'eccezione SessionAlreadyExistsException
throw new SessionAlreadyExistsException(s);
}
// si impostano i nuovi dati
s.setSessionData(sessiondata);
s.setHours(hours);
s.setSpesa(spesa);
s.setNote(note);
// si cancella l'hash vecchia
hashMap.remove(vecchiaHash);
// si inserisce quella nuova
hashMap.put(newHash, s.getID());
} else {
// si restituisce l'eccezione IDNotFoundException
throw new IDNotFoundException(ElementType.Session, sessionID);
}
}

/**
* Metodo pubblico che filtra le Session secondo vari parametri. Se non si
* vuole utilizzare un parametro impostarlo a null;
Expand Down
15 changes: 12 additions & 3 deletions QuickAgenda/src/it/valsecchi/quickagenda/data/component/Work.java
Expand Up @@ -40,8 +40,8 @@ public Work(String id, String costumerid, String _nome,String _indirizzo,
inizioLavori = iniziolavori;
fineLavori = finelavori;
completed = _completed;
// si crea l'hash unendo (id,clienteid,indirizzo,iniziolavori)
hash = Utility.getHash(costumerid + indirizzo + iniziolavori.toString());
// si crea l'hash unendo (clienteid,nome,indirizzo,iniziolavori)
hash = Utility.getHash(costumerID +nome+ indirizzo + Utility.formatCalendarToString(iniziolavori));
}

/** Costruttore di Work con Hash */
Expand All @@ -63,9 +63,18 @@ public Work(String id, String costumerid, String _nome,String _indirizzo,
*/
public static String calculateWorkHash(String costumereid,String nome,
String indirizzo, Calendar inizio) {
return Utility.getHash(costumereid + nome+ indirizzo + inizio.toString());
return Utility.getHash(costumereid + nome+ indirizzo + Utility.formatCalendarToString(inizio));
}

/**
* Metodo che ricalcola l'hash della Work. La assegna al Work e la restituisce.
* @return restituisce la nuova hash del Work
*/
public String recalculateWorkHash(){
this.hash = Utility.getHash(this.costumerID+this.nome+this.indirizzo+this.getInizioLavoriString());
return hash;
}

/** Metodo che calcola l'arco di tempo in cui il lavoro è stato attivo */
public long getWorkNumberOfDays() {
long start = inizioLavori.getTimeInMillis();
Expand Down
Expand Up @@ -275,6 +275,53 @@ public String getValidID() {
return id;
}

/**
* Metodo che modifica i dati di un Work impostando la nuova hash ed
* inserendola nella lista {@link #hashMap}.
*
* @param workID
* @param nome
* @param indirizzo
* @param costumerid
* @param iniziolavori
* @param finelavori
* @param completed
* @throws WorkAlreadyExistsException
* @throws IDNotFoundException
*/
public void modifyWork(String workID, String nome, String indirizzo,
String costumerid, Calendar iniziolavori, Calendar finelavori,
boolean completed) throws WorkAlreadyExistsException,
IDNotFoundException {
// si ricava il Work
if (worksMap.containsKey(workID)) {
Work w = worksMap.get(workID);
String vecchiaHash = w.getHash();
// viene calcolata la nuova hash
String newHash = Work.calculateWorkHash(costumerid, nome,
indirizzo, iniziolavori);
// si controlla se esiste già
if (hashMap.containsKey(newHash)) {
// si lancia l'eccezione WorkAlreadyExistsException
throw new WorkAlreadyExistsException(w);
}
// si impostano i nuovi dati
w.setNome(nome);
w.setIndirizzo(indirizzo);
w.setCostumerID(costumerid);
w.setInizioLavori(iniziolavori);
w.setFineLavori(finelavori);
w.setCompleted(completed);
// si cancella la vecchia hash
hashMap.remove(vecchiaHash);
// si aggiunge quella nuova
hashMap.put(newHash, w.getID());
} else {
// si restituisce l'eccezione IDNotFoundException
throw new IDNotFoundException(ElementType.Work, workID);
}
}

/**
* Metodo pubblico che filtra i Work secondo vari parametri. Se non si vuole
* uilizzare un parametro impostarlo a null.
Expand Down

0 comments on commit f92a4f5

Please sign in to comment.