private bool validatePictCategory(PictureCL p) { if (p.PictCatID == 0) { return(false); } string sSql = " select count(*) antal " + " from PictCategory " + " where pictCatID = :pictCatID "; NxParameterCollection pc = new NxParameterCollection(); pc.Add("pictCatID", p.PictCatID); string err = ""; DataTable dt = cdb.getData(sSql, ref err, pc); if (dt.Rows.Count == 0) { return(false); } return(Convert.ToInt32(dt.Rows[0]["antal"]) == 1); }
/// <summary> /// Private function to validate entered data /// </summary> /// <param name="p"></param> /// <param name="err"></param> /// <returns></returns> private int validatePicture(PictureCL p, bool forDelete, bool forUpdateMeta, ref string err) { CServRad cs = new CServRad(); if (p.VartOrdernr == "") { return(-1); } if (p.Radnr == 0) { return(-2); } if (forDelete) { if (p.BildNr == 0) { return(-3); } } if (forDelete || p.BildNr > 0) { int antal = cs.validteServRadBild(p.VartOrdernr, p.Radnr, p.BildNr); if (antal == 0) { return(-4); } } else if (p.BildNr == 0) { int antal = cs.validateServRad(p.VartOrdernr, p.Radnr); if (antal == 0) { return(-5); } } if (!forDelete) { if (!validatePictIdent(p.PictIdent)) { return(-6); } } if (!forDelete) { if (!validatePictCategory(p)) { return(-7); } } if (forUpdateMeta) { if (!validatePictCategory(p)) { return(-7); } } return(1); }
/// <summary> /// Calculates the next "free" picture number /// </summary> /// <param name="p"></param> /// <returns></returns> private int getNextBildNr(PictureCL p) { string sSql = " select coalesce(max(bild_nr),0) as bild_nr " + " from servrad_bild " + " where vart_ordernr = :vart_ordernr " + " and radnr = :radnr "; NxParameterCollection pc = new NxParameterCollection(); pc.Add("vart_ordernr", p.VartOrdernr); pc.Add("radnr", p.Radnr); string err = ""; DataTable dt = cdb.getData(sSql, ref err, pc); return(Convert.ToInt32(dt.Rows[0]["bild_nr"]) + 1); }
/// <summary> /// Sets the parameters to the SQL clause /// </summary> /// <param name="np"></param> /// <param name="p"></param> private void setParameters(NxParameterCollection np, PictureCL p, Boolean retrievePhoto) { long fileSize = 0; if (retrievePhoto) { np.Add("bild", getPhoto(p.PictIdent, ref fileSize)); np.Add("pictSize", fileSize); p.pictSize = fileSize; } np.Add("bild_nr", p.BildNr); np.Add("radnr", p.Radnr); np.Add("vart_ordernr", p.VartOrdernr); np.Add("pictDescript", p.Description); np.Add("pictType", "jpg"); p.pictType = "jpg"; np.Add("pictCatID", p.PictCatID); }
/// <summary> /// Updates the picture metadata. /// Note that the picture must exist, identified /// by the following properties in the picture class: /// VartOrdernr, Radnr, BidlNr. /// For performance reason this method does not evaluate /// the picture size. /// </summary> /// <param name="ident"></param> /// <param name="p"></param> /// <returns></returns> public PictureCL updatePictMetadata(string ident, PictureCL p) { CPicture cp = new CPicture(); return(cp.updatePictMetadata(ident, p)); }
/// <summary> /// Delete a picture from the database identified by /// values provided in the PictureCL parameter /// vartOrdernr /// radnr /// bildNr /// /// Note that the pictIdent parameter doesnt need to /// be filled in this case. /// /// The method returns an empty picture class if /// everything is OK /// If anything goes wrong the errCode and the errMessage /// will give further information. /// </summary> /// <param name="ident"></param> /// <param name="p"></param> /// <returns></returns> /// 2016-03-11 Pergas AB KJBO public PictureCL deletePicture(string ident, PictureCL p) { CPicture cp = new CPicture(); return(cp.deletePicture(ident, p)); }
/// <summary> /// Saves a picture to the database /// This method shall be called directory after /// a call to UploadPict /// The UploadPict gives you (upon success) /// an identity (=filename) to the upoaded file /// This identity is provided to this function /// in the PictureCL class /// Note that the BildNr field in the PictureClass /// shall always be 0 indicating that this is a /// new picture to be stored. There is no way /// to update a picture. In that case you need to delete /// the picture and, after that, add a new one /// </summary> /// <param name="ident"></param> /// <param name="p"></param> /// <returns></returns> // 2016-03-07 KJBO public PictureCL savePicture(string ident, PictureCL p) { CPicture cp = new CPicture(); return(cp.savePicture(ident, p)); }
/// <summary> /// This method returns all pictures for one servicerad /// Note that you dont get the actual picture nor the /// pictIdent. Instead you use this method for getting a /// list of available pictures (and also gets the picture /// description). /// After that you have to call GetPicture and download picture /// in turn in order to get each individual picture. /// The reason for this is performance. This method gives /// a fast list of available pictures only. /// </summary> /// <param name="ident"></param> /// <param name="vartOrdernr"></param> /// <param name="radnr"></param> /// <returns></returns> /// 2016-03-11 Pergas AB kjbo public List <PictureCL> getPicturesForServiceRad(string ident, string vartOrdernr, int radnr) { List <PictureCL> pList = new List <PictureCL>(); CReparator cr = new CReparator(); int identOK = cr.checkIdent(ident); if (identOK == -1) { PictureCL p = new PictureCL(); p.ErrCode = -10; p.ErrMessage = "Ogiltigt login"; pList.Add(p); return(pList); } string sSql = " SELECT vart_ordernr, radnr, bild_nr, bild, pictDescript, pictSize, pictType, pictCatID " + " FROM servrad_bild " + " where vart_ordernr = :vart_ordernr " + " and radnr = :radnr "; NxParameterCollection np = new NxParameterCollection(); np.Add("vart_ordernr", vartOrdernr); np.Add("radnr", radnr); string errText = ""; DataTable dt = cdb.getData(sSql, ref errText, np); int errCode = -100; if (errText == "" && dt.Rows.Count == 0) { errText = "Det finns inga bilder för aktuell servicerad"; errCode = 0; } if (errText != "") { PictureCL p = new PictureCL(); if (errText.Length > 2000) { errText = errText.Substring(1, 2000); } p.ErrCode = errCode; p.ErrMessage = errText; pList.Add(p); return(pList); } foreach (DataRow dr in dt.Rows) { PictureCL p = new PictureCL(); p.ErrCode = 0; p.ErrMessage = ""; p.VartOrdernr = dr["vart_ordernr"].ToString(); p.Radnr = Convert.ToInt32(dr["radnr"]); p.BildNr = Convert.ToInt32(dr["bild_nr"]); p.Description = dr["pictDescript"].ToString(); p.pictSize = Convert.ToInt64(dr["pictSize"]); p.pictType = dr["pictType"].ToString(); p.PictCatID = 0; if (dr["pictCatID"] != DBNull.Value) { p.PictCatID = Convert.ToInt32(dr["pictCatID"]); } pList.Add(p); } return(pList); }
/// <summary> /// Delete a picture from the database identified by /// values provided in the PictureCL parameter /// vartOrdernr /// radnr /// bildNr /// /// Note that the pictIdent parameter doesnt need to /// be filled in this case. /// /// /// The method returns an empty picture class if /// everything is OK /// If anything goes wrong the errCode and the errMessage /// will give further information. /// </summary> /// <param name="ident"></param> /// <param name="p"></param> /// <returns></returns> /// 2016-03-11 Pergas AB KJBO public PictureCL deletePicture(string ident, PictureCL p) { CReparator cr = new CReparator(); int identOK = cr.checkIdent(ident); // Creates a class to return an error PictureCL pN = new PictureCL(); if (identOK == -1) { pN.ErrCode = -10; pN.ErrMessage = "Ogiltigt login"; return(pN); } // Init variable string err = ""; int valid = validatePicture(p, true, false, ref err); if (valid == -4 || valid == -3) { pN.ErrCode = -1; pN.ErrMessage = "Det finns ingen bild lagrad för vårt ordernr : " + p.VartOrdernr + ", radnr : " + p.Radnr.ToString() + " bild nr : " + p.BildNr.ToString(); return(pN); } CServiceHuvud ch = new CServiceHuvud(); string sOpen = ch.isOpen(ident, p.VartOrdernr); if (sOpen != "1") { { deletePict(p.PictIdent); pN.ErrCode = -10; if (sOpen == "-1") { pN.ErrMessage = "Order är stängd för inmatning"; } else { pN.ErrMessage = sOpen; } return(pN); } } string sSql = ""; sSql = " delete from servrad_bild " + " where vart_ordernr = :vart_ordernr " + " and radnr = :radnr " + " and bild_nr = :bild_nr "; NxParameterCollection np = new NxParameterCollection(); setParameters(np, p, false); string errText = ""; int iRc = cdb.updateData(sSql, ref errText, np); if (errText != "") { if (errText.Length > 2000) { errText = errText.Substring(1, 2000); } pN.ErrCode = -100; pN.ErrMessage = errText; return(pN); } p.VartOrdernr = ""; p.Radnr = 0; p.BildNr = 0; p.PictIdent = ""; p.ErrCode = 0; p.ErrMessage = ""; p.Description = ""; p.PictCatID = 0; return(p); }
/// <summary> /// Saves a picture to the database /// This method shall be called directory after /// a call to UploadPict /// The UploadPict gives you (upon success) /// an identity (=filename) to the upoaded file /// This identity is provided to this function /// in the PictureCL class /// If PictureCL.bildnr = 0 indicates new picture /// Otherwise providing picture number indicates update /// /// </summary> /// <param name="ident"></param> /// <param name="p"></param> /// <returns></returns> // 2016-03-07 KJBO public PictureCL savePicture(string ident, PictureCL p) { CReparator cr = new CReparator(); int identOK = cr.checkIdent(ident); // Creates a class to return an error PictureCL pN = new PictureCL(); if (identOK == -1) { deletePict(p.PictIdent); pN.ErrCode = -10; pN.ErrMessage = "Ogiltigt login"; return(pN); } // Init variable string err = ""; int valid = validatePicture(p, false, false, ref err); if (valid == -1 || valid == -2 || valid == -5) { deletePict(p.PictIdent); pN.ErrCode = -1; pN.ErrMessage = "Kan ej hitta order"; return(pN); } if (valid == -4) { deletePict(p.PictIdent); pN.ErrCode = -1; pN.ErrMessage = "Bildnummer saknas för aktuell servicerad"; return(pN); } if (valid == -6) { pN.ErrCode = -1; pN.ErrMessage = "Bild saknas i uppladdningbiblioteket"; return(pN); } if (valid == -7) { pN.ErrCode = -1; pN.ErrMessage = "Felaktig bildkategori (PictCatID) "; return(pN); } CServiceHuvud ch = new CServiceHuvud(); string sOpen = ch.isOpen(ident, p.VartOrdernr); if (sOpen != "1") { { deletePict(p.PictIdent); pN.ErrCode = -10; if (sOpen == "-1") { pN.ErrMessage = "Order är stängd för inmatning"; } else { pN.ErrMessage = sOpen; } return(pN); } } string sSql = ""; if (p.BildNr == 0) { // This is a new bild p.BildNr = getNextBildNr(p); sSql = getInsertSQL(); } else { sSql = getUpdateSQL(true); } NxParameterCollection np = new NxParameterCollection(); setParameters(np, p, true); string errText = ""; int iRc = cdb.updateData(sSql, ref errText, np); if (errText != "") { if (errText.Length > 2000) { errText = errText.Substring(1, 2000); } pN.ErrCode = -100; pN.ErrMessage = errText; return(pN); } deletePict(p.PictIdent); return(p); }
/// <summary> /// Get a picture from the database identified by /// primary key (vartOrdernr, radnr, bildNr) /// Returns a PictCL object with the pictIdent /// field with a file name to the file being extracted /// by the server. /// If the fileName is empty or begins with -1 then /// there is an error while extracting the picture from /// the database to the temporary storage /// /// After this function is called there has to be a call /// to downloadPicture with the pictIdent as parameter /// This function returns the picture to the caller as /// a memoryStream /// </summary> /// <param name="ident"></param> /// <param name="vartOrdernr"></param> /// <param name="radnr"></param> /// <param name="bildNr"></param> /// <returns></returns> /// 2016-03-09 KJBO public PictureCL getPicture(string ident, string vartOrdernr, int radnr, int bildNr) { PictureCL p = new PictureCL(); CReparator cr = new CReparator(); int identOK = cr.checkIdent(ident); if (identOK == -1) { p.ErrCode = -10; p.ErrMessage = "Ogiltigt login"; return(p); } string sSql = " SELECT vart_ordernr, radnr, bild_nr, bild, pictDescript, pictSize, pictType, pictCatID " + " FROM servrad_bild " + " where vart_ordernr = :vart_ordernr " + " and radnr = :radnr " + " and bild_nr = :bild_nr "; NxParameterCollection np = new NxParameterCollection(); np.Add("vart_ordernr", vartOrdernr); np.Add("radnr", radnr); np.Add("bild_nr", bildNr); string errText = ""; DataTable dt = cdb.getData(sSql, ref errText, np); int errCode = -100; if (errText == "" && dt.Rows.Count == 0) { errText = "Felaktig bildidentitet"; errCode = 0; } if (errText != "") { if (errText.Length > 2000) { errText = errText.Substring(1, 2000); } p.ErrCode = errCode; p.ErrMessage = errText; return(p); } DataRow dr = dt.Rows[0]; p.ErrCode = 0; p.ErrMessage = ""; string error = ""; long fileSize = 0; if (dr["bild"] != DBNull.Value) { byte[] data = (byte[])dr["bild"]; MemoryStream ms = new MemoryStream(data); p.PictIdent = savePictToToFile(ms, ref error, ref fileSize); } if (error != "") { p.ErrCode = -1; p.ErrMessage = error; return(p); } p.VartOrdernr = dr["vart_ordernr"].ToString(); p.Radnr = Convert.ToInt32(dr["radnr"]); p.BildNr = Convert.ToInt32(dr["bild_nr"]); p.Description = dr["pictDescript"].ToString(); p.pictSize = Convert.ToInt64(dr["pictSize"]); p.pictType = dr["pictType"].ToString(); p.PictCatID = 0; if (dr["pictCatID"] != DBNull.Value) { p.PictCatID = Convert.ToInt32(dr["pictCatID"]); } return(p); }