class Add_Incident_DataService(): def __init__(self, username): logging.info("start the Add_Incident_DataService") self.incident_dao = Incident_DAO() self.new_generated_incident_id = self.incident_dao.get_count_as_int() + 1000000 self.username = username def get_new_generated_incident_id(self): ''' @rtype: int ''' return self.new_generated_incident_id def submit_form_for_new_incident(self, IncidentDate, Desciption, Latitude, Longitude): ''' @rtype: bool ''' assert isinstance(self.new_generated_incident_id, int) assert isinstance(Desciption, str) assert isinstance(Latitude, float) assert isinstance(Longitude, float) # insert into the resource table self.incident_dao.insert_incident(self.new_generated_incident_id, IncidentDate, Desciption, Latitude, Longitude, self.username)
def __init__(self): logging.debug("start the ResourceStatus_DataService()") self.userdao = User_DAO() self.incidao = Incident_DAO() self.resreqdao = ResourceRequest_DAO() self.resdao = Resource_DAO() self.repairResourcedao = RepairResource_DAO()
def test_submit_form_for_new_resource(self): aid = Add_Incident_DataService("james") icd = Incident_DAO() print "before: " + str(icd.get_count_as_int()) aid.submit_form_for_new_incident("2000-04-03", "testinci", 1.1, 2.2) print "after : " + str(icd.get_count_as_int())
def __init__(self): logging.debug("start the Search_Result_DataService()") self.res_dao = Resource_DAO() self.inci_dao = Incident_DAO() self.user_dao = User_DAO() self.res_request_dao = ResourceRequest_DAO() self.repair_resource_dao = RepairResource_DAO() self.incidentEntry = "" self.username = ""
def get_incidents_of_this_user_lists(self): ''' @rtype: list ''' icd = Incident_DAO() temp = icd.get_incident_record_by_username(self.username) result = list() for item in temp: result.append("(" + str(item[0]) + ") " + str(item[2])) return result
def test_insert_incident(self): icd = Incident_DAO() count_for_test = icd.get_count_as_int() print count_for_test icd.insert_incident(count_for_test + 1, "2016-07-03", "dummy", 14.56, 33.12, "james") print icd.get_count_as_int()
rsd.insert_resource(count + 1, "ResName" + str(count), "dummymodel", status, 44.44, 55.55, usernmae, 250.24, count % 14 + 1, unit, "tomindi" + str(i - 1), "2015-03-02", "2016-07-03") Additional_DAO(count + 1).insert_additional_esf_list(addi) Capability_DAO(count + 1).insert_capability(cap) CostUnit_DAO.insert_cost_unit("hr") CostUnit_DAO.insert_cost_unit("hour") CostUnit_DAO.insert_cost_unit("time") CostUnit_DAO.insert_cost_unit("km") CostUnit_DAO.insert_cost_unit("min") # for i in range(0, 50): count = Incident_DAO.get_count_as_int() Incident_DAO().insert_incident(count + 1, "2015-12-25", "dummyInci" + str(count + 1), 55.55, 22.22, "tomcompany" + str(i)) for i in range(0, 50): count = Incident_DAO.get_count_as_int() Incident_DAO().insert_incident(count + 1, "2015-12-25", "dummyInci" + str(count + 1), 55.55, 22.22, "tomgov" + str(i)) for i in range(0, 50): count = Incident_DAO.get_count_as_int() Incident_DAO().insert_incident(count + 1, "2015-12-25", "dummyInci" + str(count + 1), 55.55, 22.22, "muni" + str(i))
def __init__(self, username): logging.info("start the Add_Incident_DataService") self.incident_dao = Incident_DAO() self.new_generated_incident_id = self.incident_dao.get_count_as_int() + 1000000 self.username = username
def test_get_incident_position_by_incidentId(self): icd = Incident_DAO() result = icd.get_incident_position_by_incidentId(3) for row in result: print row
def test_get_record_by_inciId(self): icd = Incident_DAO() result = icd.get_incident_record_by_incidentId(1) for row in result: print row
def test_get_record_by_owner(self): icd = Incident_DAO() result = icd.get_incident_record_by_username("james") for row in result: print row
def test_get_all_records(self): icd = Incident_DAO() result = icd.get_all_incidents() for row in result: print row
class ResourceStatus_DataService(): def __init__(self): logging.debug("start the ResourceStatus_DataService()") self.userdao = User_DAO() self.incidao = Incident_DAO() self.resreqdao = ResourceRequest_DAO() self.resdao = Resource_DAO() self.repairResourcedao = RepairResource_DAO() def populate_res_in_use_tabledata(self, username): assert isinstance(username, str) sql = "SELECT ResourceID, ResourceName, owner, startDate, CurrentUser FROM Resource WHERE ResourceID in (SELECT ResourceID FROM Resource WHERE CurrentUser ='******');" sql = sql.format(username) result = run_sql_string_statement(sql) fr = list() if len(result) == 0 or len(result[0]) == 0: return fr for row in result: tmp = list() tmp.append(row[0]) tmp.append(row[1]) tmp.append(self.userdao.get_nameinfo_by_username(row[2])) tmp.append(str(row[3])) tmp.append(self.userdao.get_nameinfo_by_username(row[4])) fr.append(tmp) return fr def return_button(self, resId): assert isinstance(resId, int) sql = "UPDATE Resource SET startDate =NULL, DueDate = NULL, CurrentUser = NULL, CurrentStatus ='available' WHERE ResourceID = {0};" sql = sql.format(resId) result = run_sql_string_statement(sql) def res_request_by_username(self, username): assert isinstance(username, str) finalresult = list() sql = "SELECT ResourceID, IncidentID FROM ResourceRequest WHERE IncidentID in (SELECT IncidentID FROM Incident WHERE Incident.username ='******');" sql = sql.format(username) temp_result = run_sql_string_statement(sql) if len(temp_result) == 0 or len(temp_result[0]) == 0: return finalresult templist = list() for row in temp_result: rowtemp = list() resid = int(row[0]) incid = int(row[1]) sql = "SELECT ResourceName, Owner, DueDate FROM Resource WHERE ResourceID = {0};".format(resid) t1 = run_sql_string_statement(sql) if len(t1) == 0 or len(t1[0]) == 0: rowtemp.append(resid) rowtemp.append("") rowtemp.append("") rowtemp.append("") rowtemp.append("") rowtemp.append("") else: rowtemp.append(resid) rowtemp.append(t1[0][0]) rowtemp.append(self.incidao.get_incident_description_by_incidentId(incid)) rowtemp.append(self.userdao.get_nameinfo_by_username(t1[0][1])) rowtemp.append(str(t1[0][2])) rowtemp.append(incid) finalresult.append(rowtemp) return finalresult def cancel_button(self, resid, incid): assert isinstance(resid, int) assert isinstance(incid, int) self.resreqdao.delete_by_resIdAndInciId(resid, incid) def request_received_me(self, username): assert isinstance(username, str) finalresult = list() sql = "SELECT ResourceID, RequestID, ExpectedReturnDate, IncidentID FROM ResourceRequest WHERE ResourceID in (SELECT ResourceID from Resource WHERE Resource.Owner ='{0}');" sql = sql.format(username) temp_result = run_sql_string_statement(sql) if len(temp_result) == 0 or len(temp_result[0]) == 0: return finalresult templist = list() for row in temp_result: resid = row[0] erd = row[2] incid = row[3] reqid = row[1] t = run_sql_string_statement( "SELECT CurrentStatus, ResourceName, DueDate FROM Resource WHERE ResourceID = {0};".format( int(row[0]))) templist.append(resid) templist.append(self.resdao.get_resname_by_resourceId(int(resid))) templist.append(self.incidao.get_incident_description_by_incidentId(int(incid))) templist.append(self.userdao.get_nameinfo_by_username(username)) templist.append(str(erd)) status = t[0][0] if status != "available": templist.append("reject") else: if self.repairResourcedao.get_count_by_resId(resid) == 0: templist.append("cancelrepair") else: templist.append("deploy-reject") templist.append(incid) finalresult.append(templist) return finalresult def reject_button(self, resid, incid): assert isinstance(resid, int) assert isinstance(incid, int) self.resreqdao.delete_by_resIdAndInciId(resid, incid) def cancelrepair_button(self, resid): assert isinstance(resid, int) self.repairResourcedao.delete_by_resId(resid) def deploy_button(self, resid, erd, username, inciid): assert isinstance(resid, int) assert isinstance(inciid, int) today = str(datetime.datetime.today().date()) sql = "UPDATE Resource SET startDate ='{0}', DueDate = '{1}', CurrentStatus = 'not available', CurrentUser = '******' WHERE ResourceID = {3};" run_sql_string_statement(sql.format(today, str(erd), username, resid)) self.resreqdao.delete_by_resIdAndInciId(resid, inciid) def repairtable(self, username): assert isinstance(username, str) sql = "SELECT ResourceID, repairID, startDate, endDate FROM RepairResource WHERE ResourceID in (SELECT ResourceID FROM Resource WHERE Owner ='{0}')"; tempresult = run_sql_string_statement(sql.format(username)) finalre = list() if len(tempresult) == 0 or len(tempresult[0]) == 0: return finalre for row in tempresult: resid = int(row[0]) tm = list() tm.append(resid) tm.append(self.resdao.get_resname_by_resourceId(resid)) tm.append(str(row[2])) tm.append(str(row[3])) finalre.append(tm) return finalre
class Search_Result_DataService(): ######################################################################### # if no incident selected, ignore distance calculation and return all resources ######################################################################### def __init__(self): logging.debug("start the Search_Result_DataService()") self.res_dao = Resource_DAO() self.inci_dao = Incident_DAO() self.user_dao = User_DAO() self.res_request_dao = ResourceRequest_DAO() self.repair_resource_dao = RepairResource_DAO() self.incidentEntry = "" self.username = "" def get_incident_position(self, incident_entry): ''' @rtype: list ''' assert isinstance(incident_entry, str) temp = incident_entry[incident_entry.index("(") + 1:incident_entry.index(")")] logging.debug("incident id is " + str(temp)) return self.inci_dao.get_incident_position_by_incidentId(int(temp)) def get_esf_id(self, esf_entry): ''' @rtype: int ''' assert isinstance(esf_entry, str) temp = esf_entry[:esf_entry.index("#")] logging.debug("esf id is " + str(temp)) return int(temp) def get_resourceId_searched_by_esf(self, esfEntry): ''' @rtype: list ''' ### includes the resoruce table, cap table, and esf table assert isinstance(esfEntry, str) srd = Search_Result_DataService() esf_id = srd.get_esf_id(esfEntry) from_primary = list() from_additional = list() from_primary = self.res_dao.get_resource_lists_by_primary_esf(esf_id) from_additional = self.res_dao.get_resource_lists_by_additional_esf( esf_id) logging.debug(from_primary) logging.debug(from_additional) temp_set = set(from_primary + from_additional) return list(temp_set) def get_resourceId_searched_by_keyword(self, keyword): ''' @rtype: list ''' ### includes the resoruce table, cap table, and esf table assert isinstance(keyword, str) from_res = list() from_cap = list() from_esf = list() from_res = self.res_dao.get_resourceId_lists_by_keyword_model_or_name( keyword) from_cap = self.res_dao.get_resource_lists_by_keyword_capabilities( keyword) from_esf = self.res_dao.get_resource_lists_by_keyword_esf(keyword) logging.debug(from_res) logging.debug(from_cap) logging.debug(from_esf) temp_set = set(from_esf + from_res + from_cap) return list(temp_set) def get_distance_between_res_inci(self, inci_lat, inci_long, resoruceId): ''' @rtype: float ''' assert isinstance(inci_lat, float) assert isinstance(inci_long, float) assert isinstance(resoruceId, int) res_pos = self.res_dao.get_resource_position_by_resourceId(resoruceId) if len(res_pos) == 0: return 0.0 res_lat = res_pos[0] res_long = res_pos[1] delta_lat = radians(res_lat) - radians(inci_lat) delta_long = radians(res_long) - radians(inci_long) a = sin(delta_lat / 2)**2 + cos(radians(inci_lat)) * cos( radians(res_lat)) * sin(delta_long / 2)**2 c = 2 * atan2(sqrt(a), sqrt(1 - a)) return 6371 * c def get_resIdDict_within_distance(self, incident_entry, distance, res_id_lists): ''' @rtype: dict ''' assert isinstance(res_id_lists, list) assert isinstance(incident_entry, str) srd = Search_Result_DataService() inci_lat = srd.get_incident_position(incident_entry)[0] inci_long = srd.get_incident_position(incident_entry)[1] result = dict() for id in res_id_lists: dist = srd.get_distance_between_res_inci(inci_lat, inci_long, id) if dist <= distance: result[id] = dist sorted_result = sorted(result.items(), key=operator.itemgetter(0)) return dict(sorted_result) def populate_table_data(self, keyword, esfEntry, location_distance, incidenEntry): ''' @rtype: dict ''' assert isinstance(keyword, str) assert isinstance(incidenEntry, str) assert isinstance(esfEntry, str) assert isinstance(location_distance, int) srd = Search_Result_DataService() table_lists = list() if keyword != "" and esfEntry != "" and incidenEntry != "": self.incidentEntry = incidenEntry temp1 = srd.get_resourceId_searched_by_keyword(keyword) temp2 = srd.get_resourceId_searched_by_esf(esfEntry) tempset = set(temp1 + temp2) resource_id_list = list(tempset) resource_id_distance_dict = srd.get_resIdDict_within_distance( incidenEntry, location_distance, resource_id_list) logging.debug("final resourceid-distance mapping") logging.debug(resource_id_distance_dict) for k, v in resource_id_distance_dict.iteritems(): temp_list = list() temp_list.append(k) temp_list.append(v) logging.debug(self.res_dao.get_resource_owner_by_resourceId(k)) temp_list.append( self.user_dao.get_nameinfo_by_username( self.res_dao.get_resource_owner_by_resourceId(k))) temp_list.append( self.res_dao.get_resource_costtext_by_resourceId(k)) temp_status = self.res_dao.get_resource_status_by_resourceId(k) temp_list.append(temp_status) if temp_status == "available": temp_list.append("now") if temp_status == "not available": temp_list.append( self.res_request_dao. get_expectedreturndate_by_resourceId(k)) if temp_status == "in repair": temp_list.append( self.repair_resource_dao.get_enddate_by_resourceId(k)) table_lists.append(temp_list) if keyword == "" and esfEntry != "" and incidenEntry != "": self.incidentEntry = incidenEntry temp2 = srd.get_resourceId_searched_by_esf(esfEntry) tempset = set(temp2) resource_id_list = list(tempset) resource_id_distance_dict = srd.get_resIdDict_within_distance( incidenEntry, location_distance, resource_id_list) logging.debug("final resourceid-distance mapping") logging.debug(resource_id_distance_dict) for k, v in resource_id_distance_dict.iteritems(): temp_list = list() temp_list.append(k) temp_list.append(v) logging.debug(self.res_dao.get_resource_owner_by_resourceId(k)) temp_list.append( self.user_dao.get_nameinfo_by_username( self.res_dao.get_resource_owner_by_resourceId(k))) temp_list.append( self.res_dao.get_resource_costtext_by_resourceId(k)) temp_status = self.res_dao.get_resource_status_by_resourceId(k) temp_list.append(temp_status) if temp_status == "available": temp_list.append("now") if temp_status == "not available": temp_list.append( self.res_request_dao. get_expectedreturndate_by_resourceId(k)) if temp_status == "in repair": temp_list.append( self.repair_resource_dao.get_enddate_by_resourceId(k)) table_lists.append(temp_list) if keyword == "" and esfEntry == "" and incidenEntry != "": self.incidentEntry = incidenEntry resource_id_list = list(self.res_dao.get_all_resource_id()) resource_id_distance_dict = srd.get_resIdDict_within_distance( incidenEntry, location_distance, resource_id_list) logging.debug("final resourceid-distance mapping") logging.debug(resource_id_distance_dict) for k, v in resource_id_distance_dict.iteritems(): temp_list = list() temp_list.append(k) temp_list.append(v) logging.debug(self.res_dao.get_resource_owner_by_resourceId(k)) temp_list.append( self.user_dao.get_nameinfo_by_username( self.res_dao.get_resource_owner_by_resourceId(k))) temp_list.append( self.res_dao.get_resource_costtext_by_resourceId(k)) temp_status = self.res_dao.get_resource_status_by_resourceId(k) temp_list.append(temp_status) if temp_status == "available": temp_list.append("now") if temp_status == "not available": temp_list.append( self.res_request_dao. get_expectedreturndate_by_resourceId(k)) if temp_status == "in repair": temp_list.append( self.repair_resource_dao.get_enddate_by_resourceId(k)) table_lists.append(temp_list) if keyword == "" and esfEntry == "" and incidenEntry == "": resource_id_list = list(self.res_dao.get_all_resource_id()) logging.debug("final resourceid-distance mapping") for k in resource_id_list: temp_list = list() temp_list.append(k) temp_list.append("N.A.") logging.debug(self.res_dao.get_resource_owner_by_resourceId(k)) temp_list.append( self.user_dao.get_nameinfo_by_username( self.res_dao.get_resource_owner_by_resourceId(k))) temp_list.append( self.res_dao.get_resource_costtext_by_resourceId(k)) temp_status = self.res_dao.get_resource_status_by_resourceId(k) temp_list.append(temp_status) if temp_status == "available": temp_list.append("now") if temp_status == "not available": temp_list.append( self.res_request_dao. get_expectedreturndate_by_resourceId(k)) if temp_status == "in repair": temp_list.append( self.repair_resource_dao.get_enddate_by_resourceId(k)) table_lists.append(temp_list) return table_lists def populate_action_lists(self, table_data_lists, username): ''' @rtype: list ''' assert isinstance(table_data_lists, list) assert isinstance(username, str) result = list() for row in table_data_lists: currentStatus = row[4] logging.debug(currentStatus) owner = str( self.res_dao.get_resource_owner_by_resourceId(int(row[0]))) if currentStatus == "in repair" and owner != username: temp = list() result.append(temp) if currentStatus == "not available": temp = list() temp.append("request") result.append(temp) if currentStatus == "available" and owner != username: temp = list() temp.append("request") result.append(temp) if currentStatus == "available" and owner == username: temp = list() startdate_repair = self.repair_resource_dao.get_startdate_by_resourceId( int(row[0])) currentDate = str(datetime.datetime.today().date()) print currentDate print startdate_repair if startdate_repair == "": sd = datetime.datetime.strptime(currentDate, "%Y-%m-%d") cd = datetime.datetime.strptime(currentDate, "%Y-%m-%d") else: sd = datetime.datetime.strptime(startdate_repair, "%Y-%m-%d") cd = datetime.datetime.strptime(currentDate, "%Y-%m-%d") print sd print cd if sd < cd: temp.append("cancel repair") else: if self.incidentEntry != "": temp.append("deploy") temp.append("repair") else: temp.append("repair") result.append(temp) if self.incidentEntry == "" and owner != username: temp = list() result.append(temp) return result