示例#1
0
// GetAgeModle function for evaluate janus calls
func GetAgeModle(request *restful.Request, response *restful.Response) {
	conn, err := connectors.GetJanusConX() // get the Oracle connection
	if err != nil {
		log.Print(err)
	}
	defer conn.Close()

	sqlstring := `SELECT * FROM ocd_age_model WHERE leg = 138 AND site = 844 AND hole = 'B'`

	// Call to Struct based version
	data := &[]AgeModel{}
	GetAgeModelData(conn, sqlstring, &data)
	csvContent, err := gocsv.MarshalString(data) // Get all clients as CSV string
	if err != nil {
		log.Print(err)
	}

	response.Header().Set("Content-Type", "text/csv") // setting the content type header to text/csv
	response.Header().Set("Content-Disposition", "attachment;filename=ocdDataDownload.csv")
	response.Write([]byte(fmt.Sprintf("%v", csvContent)))
}
// DataByInterface function for evaluate janus calls
func DataByInterface(request *restful.Request, response *restful.Response, queryToCall string) {
	conn, err := connectors.GetJanusConX() // get the Oracle connection
	if err != nil {
		log.Print(err)
	}

	defer conn.Close()

	// Need a struct for the input elements
	type lshStruct struct {
		Query       string
		Leg         string
		Site        string
		Hole        string
		Core        string
		Section     string
		DepthTop    string
		DepthBottom string
	}

	// need to find the name of the last string element
	fmt.Printf("URL: %s \n", request.Request.URL.String())

	lshParams := lshStruct{Query: queryToCall, //request.PathParameter("query"),
		Leg:         request.QueryParameter("leg"),
		Site:        request.QueryParameter("site"),
		Hole:        request.QueryParameter("hole"),
		Core:        request.QueryParameter("core"),
		Section:     request.QueryParameter("section"),
		DepthTop:    request.QueryParameter("depthtop"),
		DepthBottom: request.QueryParameter("depthbottom")}

	// get the template and populate
	lshqry, err := GetSQLString(lshParams.Query)
	if err != nil {
		log.Printf("Can not find query in map, need to return error code to use for this: %s", err)
		response.AddHeader("Content-Type", "text/plain")
		response.WriteErrorString(http.StatusBadRequest, err.Error())
		return
	}

	ct, err := template.New("RDF template").Parse(lshqry)
	if err != nil {
		log.Printf("Template creation failed for query: %s", err)
		response.AddHeader("Content-Type", "text/plain")
		response.WriteErrorString(http.StatusBadRequest, err.Error())
		return
	}

	var buff = bytes.NewBufferString("")
	err = ct.Execute(buff, lshParams)
	if err != nil {
		log.Printf("Template execution failed: %s", err)
		response.AddHeader("Content-Type", "text/plain")
		response.WriteErrorString(http.StatusBadRequest, err.Error())
		return
	}

	// Need to log this to a "query" log so I can know what is called and how often
	//log.Print(string(buff.Bytes()))

	// Call to interface based version
	csvContent, err := GetJanusCSV(conn, string(buff.Bytes()))
	if err != nil {
		log.Print(err)
		response.AddHeader("Content-Type", "text/plain")
		response.WriteErrorString(http.StatusBadRequest, err.Error())
		return
	}

	if csvContent == "" {
		response.AddHeader("Content-Type", "text/plain")
		response.WriteErrorString(http.StatusNoContent, "No data found for request")
		return
	}

	response.Header().Set("Content-Type", "text/csv") // setting the content type header to text/csv
	response.Header().Set("Content-Disposition", "attachment;filename=ocdDataDownload.csv")
	response.Write([]byte(fmt.Sprintf("%v", csvContent)))
}