class TaskOrderRequestParser:
    def __init__(self):
        self.request_parser = TcpRequestParser()

    def getType(self, request):
        return  self.request_parser.parseRequestType(request)

    def parseRequest(self, request):
        return self.request_parser.parse(request)

    def handleDefaultRequestType(self, request):
        self.request_parser.parseDefaultType()

    def createResponse(self, status, messsage):
        doc = Document()
        root_response = doc.createElement('ProductionResponse')
        root_response.setAttribute("creationSuccessful", str(status))
        if status:
            task = messsage
            root_response.setAttribute("orderName", task.name)
            for order in task.orders:
                order_element = doc.createElement('order')
                order_element.appendChild(doc.createTextNode(str(order)))
                root_response.appendChild(order_element)
        else:
            root_response.setAttribute("reason", messsage)
        doc.appendChild(root_response)
        return doc.toxml(encoding='utf-8')
class TaskOrderRequestHandler:
    def __init__(self, app):
        self.app = app
        self.request_parser = TcpRequestParser()

    """
    Response should NOT be None !!!
    """
    def handleRequest(self, request):
        response = None
        try:
            type = self.request_parser.parseRequestType(request)

            if type.upper() == "NC_DEVICE_LOADING_UNLOADING":
                orders = self.request_parser.parseNcOrderTask(request, "LOADING_UNLOADING")
                response = self.crateAndAddNewTask(orders)
            elif type.upper() == "NC_DEVICE_LOADING": # NC Devices (loading operation)
                orders = self.request_parser.parseNcOrderTask(request, "LOADING")
                response = self.crateAndAddNewTask(orders)
            elif type.upper() == "NC_DEVICE_UNLOADING":# NC Devices (unloading operation)
                orders = self.request_parser.parseNcOrderTask(request, "UNLOADING")
                response = self.crateAndAddNewTask(orders)
            elif type.upper() == "ROBOT_ASSEMBLE": # Robot Assemble Line
                orders = self.request_parser.parseRobotAssembleTask(request, "Robot Assemble")
                response = self.crateAndAddNewTask(orders)
            elif type.upper() == "TRIGGER": # Operation trigger
                order_name, device_no = self.request_parser.parseProductionOrderOperationTrigger(request)
                response = self.triggerBlockedTask(order_name, device_no)
            else:
                response = self.request_parser.parseDefaultType(type)
        except Exception, e:
            response = self.request_parser.createGeneralUnknownResponse(str(e))

        return response