def get_query_jrxmlFile_from_server(self, file_content): query = "" xmldoc = JasperXmlReport(BytesIO(file_content)) list_query = xmldoc.get_query() if list_query: query = list_query[0].text return query
def get_reports_list_from_server(self, force=False): ret = {} s = Search(self.session) result = s.search(path=self.doc.get("jasper_report_root_path").strip(), type="reportUnit") reports = result.getDescriptor().json_descriptor() for report in reports: ics = [] d = Details(self.session, report.get("uri")) r_details = d.details(expanded=False) r_param = r_details.getDescriptor().json_descriptor() uri = r_param[0].get('jrxml').get('jrxmlFileReference').get('uri') file_content = self.get_jrxml_from_server(uri) xmldoc = JasperXmlReport(BytesIO(file_content)) params = xmldoc.get_params() query = self.get_query_jrxmlFile_from_server(file_content) for param in params: pname = param.xpath('./@name') pclass = param.xpath('./@class') ptype = pclass[0].split(".") c = len(ptype) - 1 ics.append({"label":pname[0], "type":ptype[c].lower()}) updateDate = report.get("updateDate", None) if not updateDate: updateDate = report.get("creationDate", frappe.utils.now()) ret[report.get("label")] = {"uri":report.get("uri"), "inputControls": ics, "updateDate": updateDate, "queryString": query} return ret
def process_childs(self): docs = frappe.get_all("File", fields=["file_name", "file_url"], filters={"attached_to_name": self.dn, "attached_to_doctype": self.dt, "name": self.parent}) if not docs: frappe.msgprint(_("Add a report first."), raise_exception=True) for doc in docs: jrxml_ext = get_extension(doc.file_name) if jrxml_ext == "jrxml": #jrxml_os_path = self.path_join(self.jasper_path, doc.file_url[1:]) jrxml_os_path = self.path_join(self.jasper_path, get_file_path(doc.file_url)) xmldoc = JasperXmlReport(jrxml_os_path) if (self.ext!="properties" and self.ext != "xml"): image_path = xmldoc.get_image_path_from_jrxml(self.fname) self.file_path= self.path_join(self.compiled_path, os.path.normpath(image_path)) elif (self.ext == "xml"): xmlname = xmldoc.getProperty("XMLNAME") if xmlname: xname = xmlname + ".xml" if xname != self.fname: frappe.msgprint(_("This report does't have %s as file source." % (self.fname,)),raise_exception=True) self.file_path = self.path_join(self.compiled_path, os.path.normpath(self.fname)) else: frappe.msgprint(_("This report does't have %s as file source." % (self.fname,)),raise_exception=True) else: value = xmldoc.get_attrib("resourceBundle") if not value or value not in self.fname: frappe.msgprint(_("This report does't have %s as properties." % (self.fname,)),raise_exception=True) self.file_path = self.path_join(self.compiled_path, os.path.normpath(self.fname)) break else: frappe.msgprint(_("Add a file for this report first."),raise_exception=True)
def before_save(self, method=None): self.jasper_doctype = None if not frappe.utils.strip(self.jasper_doctype) else self.jasper_doctype self.report = None if not frappe.utils.strip(self.report) else self.report if not self.jasper_param_message: try: self.jasper_param_message = frappe.db.get_values_from_single(["jasper_param_message"], None, "JasperServerConfig")[0][0].format(report=self.jasper_report_name, user=frappe.local.session['user']) except: self.jasper_param_message = "" #check if Jasper is configurated use_jasper_server = frappe.db.get_values_from_single(["use_jasper_server"], None, "JasperServerConfig")[0][0] if use_jasper_server == "None": frappe.throw(_("You need to configure Jasper first.")) return if check_root_exists(self.doctype, self.name): rootquery = '' self.query = '' jrxml_path = _get_jrxml_root_path(self) xmldoc = JasperXmlReport(jrxml_path) xmlname = check_if_xPath_exists(xmldoc) if xmlname and not check_for_report_xPath(xmldoc, xmlname, self): frappe.throw(_("Import %s for report %s first." % (xmlname + ".xml",self.jasper_report_name))) subreportquerys = getSubReportsQuery(xmldoc, self) subquery = '' for subreportquery in subreportquerys: subquery = subquery + subreportquery.get("name") + ":\n" + subreportquery.get("query") + "\n" if xmldoc.queryString or subquery: self.query = xmldoc.name + ":\n" + xmldoc.queryString + "\n" + subquery #give feedback to the user shown related params params = xmldoc.get_params_from_xml() #get total number of parameters to concatenate with name of parameter is_copy = "Other" action_type = "Ask" for param in params: pname = param.xpath('./@name') pclass = param.xpath('./@class') ptype = pclass[0].split(".") c = len(ptype) - 1 if check_param_exists(self, pname[0]): break if check_queryString_param(xmldoc.queryString, pname[0]): is_copy = "Is for where clause" action_type = "Automatic" #param_type = param_type_java_conversion.get(ptype[c]) self.append("jasper_parameters", {"__islocal": True, "jasper_param_name":pname[0], "jasper_param_type": ptype[c],#.lower().capitalize() "jasper_param_action": action_type, "param_expression":"In", "is_copy":is_copy, "name": self.name + "_" + pname[0]}) self.query = rootquery + self.query return #if jrxml file was removed then prepare to remove all associated images and params given feedback to the user if self.jasper_report_origin.lower() == "localserver": self.jasper_parameters = [] return
def getSubReportsQuery(xmlroot, doc): subquery = [] check_for_report_images(xmlroot, doc) jasper_path = get_jasper_path(doc.jasper_all_sites_report) subreports = xmlroot.subreports for path_name in subreports: report_path = path_name[:-7] + ".jrxml" file_path = frappe.utils.get_path(doc.name, report_path, base=jasper_path) try: xmldoc = JasperXmlReport(file_path) subquery.append({"name": xmldoc.name, "query": xmldoc.queryString}) subquery.extend(xmldoc.datasets) #check if the subreport has subreports too subquery.extend(getSubReportsQuery(xmldoc, doc)) except: frappe.msgprint(_("Subreport %s is missing." % (report_path)), raise_exception=True) return subquery
def process_jrxmls(self): rname = check_if_jrxml_exists_db(self.dt, self.dn, self.fname, self.parent) if rname or check_root_exists(self.dt, self.dn): frappe.msgprint(_( "Remove first the file (%s) associated with this document or (%s) is a wrong parent." % (rname, rname)), raise_exception=True) jrxml_path = get_jrxml_path(self.jasper_path, self.dn) self.file_path = self.path_join(jrxml_path, self.fname) #check if the parent has this jrxml as child docs = frappe.get_all("File Data", fields=["file_name", "file_url"], filters={ "attached_to_name": self.dn, "attached_to_doctype": self.dt, "name": self.parent }) if docs: xmldoc = JasperXmlReport( self.path_join(jrxml_path, docs[0].file_name)) for sub in xmldoc.subreports: s = sub.rsplit("/", 1) if len(s) > 1: if not (s[1][:-7] == self.fname[:-6]): frappe.msgprint( _("The report %s is not a subreport of %s." % (self.fname[:-6], docs[0].file_name[:-6])), raise_exception=True) elif not (sub[:-7] == self.fname[:-6]): frappe.msgprint( _("The report %s is not a subreport of %s." % (self.fname[:-6], docs[0].file_name[:-6])), raise_exception=True) if not xmldoc.subreports: frappe.msgprint(_("The report %s is not a subreport of %s." % (self.fname[:-6], docs[0].file_name[:-6])), raise_exception=True)
def before_save(self, method=None): self.jasper_doctype = None if not frappe.utils.strip( self.jasper_doctype) else self.jasper_doctype self.report = None if not frappe.utils.strip( self.report) else self.report if not self.jasper_param_message: self.jasper_param_message = frappe.db.get_values_from_single( ["jasper_param_message"], None, "JasperServerConfig")[0][0].format( report=self.jasper_report_name, user=frappe.local.session['user']) #check if Jasper is configurated use_jasper_server = frappe.db.get_values_from_single( ["use_jasper_server"], None, "JasperServerConfig")[0][0] if use_jasper_server == "None": frappe.throw(_("You need to configure Jasper first.")) return if check_root_exists(self.doctype, self.name): rootquery = '' self.query = '' jrxml_path = _get_jrxml_root_path(self) xmldoc = JasperXmlReport(jrxml_path) xmlname = check_if_xPath_exists(xmldoc) if xmlname and not check_for_report_xPath(xmldoc, xmlname, self): frappe.throw( _("Import %s for report %s first." % (xmlname + ".xml", self.jasper_report_name))) subreportquerys = getSubReportsQuery(xmldoc, self) subquery = '' for subreportquery in subreportquerys: subquery = subquery + subreportquery.get( "name") + ":\n" + subreportquery.get("query") + "\n" if xmldoc.queryString or subquery: self.query = xmldoc.name + ":\n" + xmldoc.queryString + "\n" + subquery #give feedback to the user shown related params params = xmldoc.get_params_from_xml() #get total number of parameters to concatenate with name of parameter is_copy = "Other" action_type = "Ask" for param in params: pname = param.xpath('./@name') pclass = param.xpath('./@class') ptype = pclass[0].split(".") c = len(ptype) - 1 if check_param_exists(self, pname[0]): break if check_queryString_param(xmldoc.queryString, pname[0]): is_copy = "Is for where clause" action_type = "Automatic" #param_type = param_type_java_conversion.get(ptype[c]) self.append( "jasper_parameters", { "__islocal": True, "jasper_param_name": pname[0], "jasper_param_type": ptype[c], #.lower().capitalize() "jasper_param_action": action_type, "param_expression": "In", "is_copy": is_copy, "name": self.name + "_" + pname[0] }) self.query = rootquery + self.query return #if jrxml file was removed then prepare to remove all associated images and params given feedback to the user if self.jasper_report_origin.lower() == "localserver": self.jasper_parameters = [] return
def make_content_jrxml(self, name): xmldoc = JasperXmlReport(BytesIO(self.content)) xmldoc.change_subreport_expression_path() self.scriptlet = xmldoc.get_attrib("scriptletClass") #TODO if not self.scriptlet: pass xmldoc.change_path_images() xmldoc.setProperty("parent", self.parent) xmldoc.setProperty("jasperId", name) self.content = xmldoc.toString()
def make_content_jrxml(self, name): #import re xmldoc = JasperXmlReport(BytesIO(self.content)) xmldoc.change_subreport_expression_path() self.scriptlet = xmldoc.get_attrib("scriptletClass") #TODO if not self.scriptlet: pass xmldoc.change_path_images() xmldoc.setProperty("parent", self.parent) xmldoc.setProperty("jasperId", name) self.content = xmldoc.toString()