From: Florian L. <fle...@us...> - 2005-10-21 11:09:39
|
Update of /cvsroot/magicmap/magicmapserver/src/net/sf/magicmap/server/facade In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5446/src/net/sf/magicmap/server/facade Added Files: PositionFacade2.java Log Message: new Positionfacade for hashing positions --- NEW FILE: PositionFacade2.java --- package net.sf.magicmap.server.facade; import java.rmi.RemoteException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Date; import java.util.Iterator; import javax.jdo.Extent; import javax.jdo.PersistenceManager; import javax.jdo.Query; import javax.jdo.Transaction; import net.sf.magicmap.db.AccessPoint; import net.sf.magicmap.db.Client; import net.sf.magicmap.db.Map; import net.sf.magicmap.db.Position; import net.sf.magicmap.db.ScanResult; import net.sf.magicmap.db.Session; import net.sf.magicmap.server.dto.AccessPointDTO; import net.sf.magicmap.server.dto.ClientDTO; import net.sf.magicmap.server.dto.PositionDTO; import net.sf.magicmap.server.dto.SignalCharacterDTO; import net.sf.magicmap.server.dto.SimpleScanResultDTO; import net.sf.magicmap.server.exception.MapException; import net.sf.magicmap.server.exception.SessionException; import net.sf.magicmap.server.interfaces.PositionFacadeInterface; import net.sf.magicmap.server.utils.JDOUtil; import org.apache.log4j.Category; /** * author schweige * date 03.12.2004 * copyright (C) 2004 Martin Schweigert, Tobias Hübner * * * This program 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. */ public class PositionFacade2 implements PositionFacadeInterface { protected transient Category logger = Category.getInstance(this.getClass()); /* (non-Javadoc) * @see net.sf.magicmap.server.facade.interfaces.PositionFacadeInterface#createPosition(long, java.lang.String, int, int, net.sf.magicmap.server.facade.dto.SignalCharacter, java.lang.String) */ public void createOrUpdatePosition(long sessionId, String mapName, int positionX, int positionY, SignalCharacterDTO character, String positionName, boolean fixed) throws SessionException, MapException{ this.logger.info("createOrUpdatePosition()"); createOrUpdatePositionInternal(sessionId, mapName, positionX, positionY, character, positionName, fixed); this.logger.info("createOrUpdatePosition() - done"); } /** * @param sessionId * @param mapName * @param positionX * @param positionY * @param character * @param positionName * @param pm * @param tx * @throws SessionException * @throws MapException */ private void createOrUpdatePositionInternal(long sessionId, String mapName, int positionX, int positionY, SignalCharacterDTO character, String positionName, boolean fixed) throws SessionException, MapException{ Session session = checkSession(sessionId, JDOUtil.pmfactory.getPersistenceManager()); Client client = session.getClient(); if (client == null){ throw new SessionException("Ungültige Session"); return; } if (character == null || character.getSimpleScanResults() == null || character.getSimpleScanResults().length == 0){ throw new MapException("Signalcharacter enthält keine Daten"); return; } if (positionName == null || "".equals(positionName)){ throw new MapException("Name der Position darf nicht leer sein"); return; } Map map = checkMap(mapName, positionX, positionY, JDOUtil.pmfactory.getPersistenceManager()); Position pos = checkPosition(positionName, map, pm); if (pos == null){ pos = new Position(map, positionX, positionY, positionName); pm.makePersistent(pos); } pos.setDeleted(false); pos.setPosX(positionX); pos.setPosY(positionY); pos.setFixed(fixed); //pos.setClient(client); setNewSignalCharacter(character, pm, client, pos); } /** * @param character * @param pm * @param client * @param pos * @throws MapException */ private void setNewSignalCharacter(SignalCharacterDTO character, PersistenceManager pm, Client client, Position pos) throws MapException{ // alle Scanresults dieser Position löschen pm.deletePersistentAll(pos.getScanResults()); SimpleScanResultDTO[] dtos = character.getSimpleScanResults(); if (dtos != null){ for (int i = 0; i < dtos.length; i++){ AccessPoint ap = findOrCreateAccessPoint(dtos[i].getMacAP(), pm); //@TODO think about? ap.setSsid(dtos[i].getSsid()); ScanResult sr = new ScanResult(client, ap, dtos[i].getSignalLevel(), dtos[i].getNoise(), new Timestamp(System .currentTimeMillis())); //@TODO use lastSeen pm.makePersistent(sr); sr.setPosition(pos); } } } /** * @param macAP * @param pm * @return * @throws MapException * @throws s */ private AccessPoint findOrCreateAccessPoint(String macAP, PersistenceManager pm) throws MapException{ try{ AccessPoint result = null; Collection results; Extent e = pm.getExtent(AccessPoint.class, true); Query query = pm.newQuery(e, "mac == myMac"); query.declareParameters("java.lang.String myMac"); query.declareImports("import java.lang.String;"); results = (Collection) query.execute(macAP); if (results != null && !results.isEmpty()){ result = (AccessPoint) results.iterator().next(); } else{ result = new AccessPoint(macAP); pm.makePersistent(result); } return result; } catch (Exception e){ pm.currentTransaction().rollback(); throw new MapException("", e); } } /** * @param positionName * @param map * @param pm * @return */ private Position checkPosition(String positionName, Map map, PersistenceManager pm){ Position result = null; Collection results; Extent e = pm.getExtent(Position.class, true); Query query = pm.newQuery(e, "name == myName && map == myMap"); query.declareParameters("java.lang.String myName, net.sf.magicmap.db.Map myMap"); query.declareImports("import java.lang.String; import net.sf.magicmap.db.Map;"); results = (Collection) query.execute(positionName, map); if (results != null && !results.isEmpty()){ result = (Position) results.iterator().next(); } return result; } /** * @param mapName * @param positionX * @param positionY * @param pm * @return * @throws MapException */ private Map checkMap(String mapName, int positionX, int positionY, PersistenceManager pm) throws MapException{ Map result = checkMap(mapName, pm); if (positionX < 0 || positionY < 0 || positionX > (result.getImageWidth().intValue() - 1) || positionY > (result.getImageHeight().intValue() - 1)){ throw new MapException("Position ist nicht auf der Karte"); return null; } return result; } /** * @param mapName * @param pm * @return * @throws MapException */ private Map checkMap(String mapName, PersistenceManager pm) throws MapException{ Map result = null; Collection results; Extent e = pm.getExtent(Map.class, true); Query query = pm.newQuery(e, "name == myName"); query.declareParameters("java.lang.String myName"); results = (Collection) query.execute(mapName); if (results != null && !results.isEmpty()){ result = (Map) results.iterator().next(); } else{ pm.currentTransaction().rollback(); throw new MapException("Karte mit dem namen:" + mapName + " existiert nicht"); } return result; } /** * @param sessionId * @param tx * @throws SessionException */ private Session checkSession(long sessionId, PersistenceManager pm) throws SessionException{ Session result = null; Collection results; Extent e = pm.getExtent(Session.class, true); Query query = pm.newQuery(e, "id == myId"); query.declareParameters("java.lang.Long myId"); results = (Collection) query.execute(new Long(sessionId)); if (results != null && !results.isEmpty()){ result = (Session) results.iterator().next(); } else{ pm.currentTransaction().rollback(); throw new SessionException("Session mit der id:" + sessionId + " existiert nicht"); } return result; } /** * @param sessionId * @param tx * @throws SessionException */ private Position checkPosition(long positionId, PersistenceManager pm) throws MapException{ Position result = null; Collection results; Extent e = pm.getExtent(Position.class, true); Query query = pm.newQuery(e, "id == myId"); query.declareParameters("java.lang.Long myId"); results = (Collection) query.execute(new Long(positionId)); if (results != null && !results.isEmpty()){ result = (Position) results.iterator().next(); } else{ pm.currentTransaction().rollback(); throw new MapException("Position mit der id:" + positionId + " existiert nicht"); } return result; } /** * @param client * @param map * @return */ private Position getPositionForClientAndMap(Client client, Map map, PersistenceManager pm){ Position result = null; Collection results; Extent e = pm.getExtent(Position.class, true); Query query = pm.newQuery(e, "client == myClient && map == myMap"); query.declareParameters("net.sf.magicmap.db.Client myClient, net.sf.magicmap.db.Map myMap"); query.declareImports("import net.sf.magicmap.db.Client; import net.sf.magicmap.db.Map;"); results = (Collection) query.execute(client, map); if (results != null && !results.isEmpty()){ result = (Position) results.iterator().next(); } return result; } /** * @param client * @param map * @return */ private Position getPositionForApAndMap(AccessPoint ap, Map map, PersistenceManager pm){ Position result = null; Collection results; Extent e = pm.getExtent(Position.class, true); Query query = pm.newQuery(e, "accessPoint == myAp && map == myMap"); query.declareParameters("net.sf.magicmap.db.AccessPoint myAp, net.sf.magicmap.db.Map myMap"); query.declareImports("import net.sf.magicmap.db.AccessPoint; import net.sf.magicmap.db.Map;"); results = (Collection) query.execute(ap, map); if (results != null && !results.isEmpty()){ result = (Position) results.iterator().next(); } return result; } /** * @param sessionId * @param tx * @throws SessionException */ private Client checkClient(String mac, PersistenceManager pm) throws MapException{ if (mac == null || "".equals(mac)){ pm.currentTransaction().rollback(); throw new MapException("Client mac darf nicht leer sein"); } Client result = null; Collection results; Extent e = pm.getExtent(Client.class, true); Query query = pm.newQuery(e, "mac == myMac"); query.declareParameters("java.lang.String myMac"); results = (Collection) query.execute(mac); if (results != null && !results.isEmpty()){ result = (Client) results.iterator().next(); } else{ pm.currentTransaction().rollback(); throw new MapException("Client mit der mac-adresse:" + mac + " existiert nicht"); } return result; } /* (non-Javadoc) * @see net.sf.magicmap.server.facade.interfaces.PositionFacadeInterface#createOrUpdateClientPosition(long, java.lang.String, int, int, net.sf.magicmap.server.facade.dto.SignalCharacter, java.lang.String) */ public void createOrUpdateClientPosition(long sessionId, String mapName, int positionX, int positionY, SignalCharacterDTO character, String clientMac, boolean fixed) throws SessionException, MapException{ this.logger.info("createOrUpdateClientPosition()"); PersistenceManager pm = null; Transaction tx = null; try{ pm = JDOUtil.pmfactory.getPersistenceManager(); tx = pm.currentTransaction(); tx.begin(); int count = 0; while (count < 3){ try{ createOrUpdateClientPositionInternal(sessionId, mapName, positionX, positionY, character, clientMac, fixed, pm); break; } catch (javax.jdo.JDODataStoreException jdoEx){ if (tx.isActive()){ tx.rollback(); } count++; tx.begin(); } } if (count == 3){ if (tx.isActive()){ tx.rollback(); } System.out.println("createOrUpdateClientPosition - GAVE UP - repeated 3 times"); } if (tx.isActive()){ tx.commit(); } } catch (Exception e){ e.printStackTrace(); if (e instanceof MapException){ throw (MapException) e; } if (e instanceof SessionException){ throw (SessionException) e; } } finally{ if (tx.isActive()){ tx.rollback(); } JDOUtil.closePM(pm); } this.logger.info("createOrUpdateClientPosition() - done"); } /** * @param sessionId * @param mapName * @param positionX * @param positionY * @param character * @param clientMac * @param pm * @throws SessionException * @throws MapException */ private void createOrUpdateClientPositionInternal(long sessionId, String mapName, int positionX, int positionY, SignalCharacterDTO character, String clientMac, boolean fixed, PersistenceManager pm) throws SessionException, MapException{ checkSession(sessionId, pm); Map map = checkMap(mapName, positionX, positionY, pm); Client client = checkClient(clientMac, pm); Position pos = getPositionForClientAndMap(client, map, pm); if (pos == null){ pos = new Position(map, positionX, positionY, client.getMac()); pm.makePersistent(pos); } pos.setClient(client); pos.setDeleted(false); pos.setName(client.getMac()); pos.setPosX(positionX); pos.setPosY(positionY); pos.setFixed(fixed); setNewSignalCharacter(character, pm, client, pos); } /* (non-Javadoc) * @see net.sf.magicmap.server.facade.interfaces.PositionFacadeInterface#createOrUpdateAccessPosition(long, java.lang.String, int, int, net.sf.magicmap.server.facade.dto.SignalCharacter, java.lang.String) */ public void createOrUpdateAccessPosition(long sessionId, String mapName, int positionX, int positionY, String accessPointMac, boolean fixed) throws SessionException, MapException{ this.logger.info("createOrUpdateAccessPosition()"); PersistenceManager pm = null; Transaction tx = null; try{ pm = JDOUtil.pmfactory.getPersistenceManager(); tx = pm.currentTransaction(); tx.begin(); checkSession(sessionId, pm); Map map = checkMap(mapName, positionX, positionY, pm); AccessPoint ap = findOrCreateAccessPoint(accessPointMac, pm); Position pos = getPositionForApAndMap(ap, map, pm); if (pos == null){ pos = new Position(map, positionX, positionY, ap.getMac()); pm.makePersistent(pos); } pos.setAccessPoint(ap); pos.setDeleted(false); pos.setName(ap.getMac()); pos.setPosX(positionX); pos.setPosY(positionY); pos.setFixed(fixed); tx.commit(); } catch (Exception e){ e.printStackTrace(); if (e instanceof MapException){ throw (MapException) e; } if (e instanceof SessionException){ throw (SessionException) e; } } finally{ if (tx.isActive()){ tx.rollback(); } JDOUtil.closePM(pm); } this.logger.info("createOrUpdateAccessPosition() - done"); } /* (non-Javadoc) * @see net.sf.magicmap.server.facade.interfaces.PositionFacadeInterface#movePosition(long, long, int, int) */ public void movePosition(long sessionId, long positionId, int newPositionX, int newPositionY, boolean fixed) throws SessionException, MapException{ this.logger.info("movePosition()"); PersistenceManager pm = null; Transaction tx = null; try{ pm = JDOUtil.pmfactory.getPersistenceManager(); tx = pm.currentTransaction(); tx.begin(); checkSession(sessionId, pm); Position pos = checkPosition(positionId, pm); Map map = pos.getMap(); if (newPositionX < 0 || newPositionY < 0 || newPositionX > (map.getImageWidth().intValue() - 1) || newPositionY > (map.getImageHeight().intValue() - 1)){ pm.currentTransaction().rollback(); throw new MapException("Position ist nicht auf der Karte"); } pos.setPosX(newPositionX); pos.setPosY(newPositionY); pos.setFixed(fixed); tx.commit(); } catch (Exception e){ e.printStackTrace(); if (e instanceof MapException){ throw (MapException) e; } if (e instanceof SessionException){ throw (SessionException) e; } } finally{ if (tx.isActive()){ tx.rollback(); } JDOUtil.closePM(pm); } this.logger.info("movePosition() - done"); } /* (non-Javadoc) * @see net.sf.magicmap.server.facade.interfaces.PositionFacadeInterface#deletePosition(long, long) */ public void deletePosition(long sessionId, long positionId) throws SessionException, MapException{ this.logger.info("deletePosition()"); PersistenceManager pm = null; Transaction tx = null; try{ pm = JDOUtil.pmfactory.getPersistenceManager(); tx = pm.currentTransaction(); tx.begin(); checkSession(sessionId, pm); Position pos = checkPosition(positionId, pm); pos.setDeleted(true); tx.commit(); } catch (Exception e){ e.printStackTrace(); if (e instanceof MapException){ throw (MapException) e; } if (e instanceof SessionException){ throw (SessionException) e; } } finally{ if (tx.isActive()){ tx.rollback(); } JDOUtil.closePM(pm); } this.logger.info("deletePosition() - done"); } /* (non-Javadoc) * @see net.sf.magicmap.server.facade.interfaces.PositionFacadeInterface#getPositionsForMapSince(long, java.lang.String, long) */ public PositionDTO[] getPositionsForMapSince(long sessionId, String mapName, long timeStamp) throws MapException, SessionException{ this.logger.info("getPositionsForMapSince() " + timeStamp); PersistenceManager pm = null; Transaction tx = null; ArrayList result = new ArrayList(); try{ pm = JDOUtil.pmfactory.getPersistenceManager(); tx = pm.currentTransaction(); tx.begin(); checkSession(sessionId, pm); Map map = checkMap(mapName, pm); Extent e = pm.getExtent(Position.class, true); String filter = "map == myMap && lastModified > myLastModified"; if (timeStamp == -1){ filter += " && deleted == 0"; } Query query = pm.newQuery(e, filter); query.declareParameters("net.sf.magicmap.db.Map myMap, java.lang.Long myLastModified"); query.declareImports("import net.sf.magicmap.db.Map; import java.lang.Long;"); Collection results = null; results = (Collection) query.execute(map, new Long(timeStamp)); if (results != null){ Iterator it = results.iterator(); while (it.hasNext()){ Position pos = (Position) it.next(); // bei Timestamp -1 nur nicht gelöschte Positionen zurückliefern PositionDTO dto = new PositionDTO(); AccessPoint ap = pos.getAccessPoint(); if (ap != null){ AccessPointDTO apDTO = new AccessPointDTO(); apDTO.setMac(ap.getMac()); apDTO.setId(new Long(ap.getId())); apDTO.setSsid(ap.getSsid()); dto.setAccessPoint(apDTO); } Client client = pos.getClient(); if (client != null){ ClientDTO clientDTO = new ClientDTO(); clientDTO.setMac(client.getMac()); clientDTO.setId(new Long(client.getId())); clientDTO.setName(client.getName()); dto.setClient(clientDTO); } dto.setId(new Long(pos.getId())); dto.setDeleted(pos.isDeleted()); dto.setLastModified(new Long(pos.getLastModified())); dto.setPosX(new Integer(pos.getPosX())); dto.setPosY(new Integer(pos.getPosY())); dto.setFixed(pos.isFixed()); dto.setName(pos.getName()); dto.setCharacter(new SignalCharacterDTO()); // Scanresults holen Collection scanResults = pos.getScanResults(); if (scanResults != null){ Iterator scanIt = scanResults.iterator(); while (scanIt.hasNext()){ ScanResult sr = (ScanResult) scanIt.next(); Date lastSeen = null; if (sr.getScantime() != null){ lastSeen = new Date(sr.getScantime().getTime()); } SimpleScanResultDTO ssr = new SimpleScanResultDTO(); //ssr.setLastSeen(lastSeen); //TODO: last seen benutzen ssr.setMacAP(sr.getAccessPoint().getMac()); ssr.setNoise(sr.getNoiseLevel()); ssr.setSignalLevel(sr.getSignalLevel()); ssr.setSsid(sr.getAccessPoint().getSsid()); ArrayList list; if (dto.getCharacter().getSimpleScanResults() == null){ list = new ArrayList(); } else{ list = new ArrayList(Arrays.asList(dto.getCharacter().getSimpleScanResults())); } list.add(ssr); dto.getCharacter().setSimpleScanResults((SimpleScanResultDTO[]) list.toArray(new SimpleScanResultDTO[0])); } } result.add(dto); } } tx.commit(); } catch (Exception e){ e.printStackTrace(); if (e instanceof MapException){ throw (MapException) e; } if (e instanceof SessionException){ throw (SessionException) e; } } finally{ if (tx.isActive()){ tx.rollback(); } JDOUtil.closePM(pm); } this.logger.info("getPositionsForMapSince() - done "); return (PositionDTO[]) result.toArray(new PositionDTO[0]); } /* (non-Javadoc) * @see net.sf.magicmap.server.facade.interfaces.PositionFacadeInterface#getPositionsForClientOnMap(long, java.lang.String, java.lang.String) */ public String getPositionForClientOnMap(long sessionId, String mapName, String clientMac) throws MapException, SessionException, RemoteException { this.logger.info("getPositionsForClientOnMap() "); PersistenceManager pm = null; String positionResult = null; Transaction tx = null; try{ pm = JDOUtil.pmfactory.getPersistenceManager(); tx = pm.currentTransaction(); tx.begin(); checkSession(sessionId, pm); Map map = checkMap(mapName, pm); checkClient(clientMac, pm); Extent e = pm.getExtent(Position.class, true); String filter = "map == myMap && name == myName"; Query query = pm.newQuery(e, filter); query.declareParameters("net.sf.magicmap.db.Map myMap, java.lang.String myName"); query.declareImports("import net.sf.magicmap.db.Map; import java.lang.String;"); Collection results = null; results = (Collection) query.execute(map, clientMac); if (results != null){ Iterator it = results.iterator(); if(it.hasNext()){ Position pos = (Position) it.next(); positionResult = String.valueOf(pos.getPosX())+ "#" + String.valueOf(pos.getPosY()) + "#" + String.valueOf(pos.isFixed()); } } tx.commit(); } catch (Exception e){ e.printStackTrace(); if (e instanceof MapException){ throw (MapException) e; } if (e instanceof SessionException){ throw (SessionException) e; } } finally{ if (tx.isActive()){ tx.rollback(); } JDOUtil.closePM(pm); } this.logger.info("getPositionsForClientOnMap() - done "); return positionResult; } } |