/* Items Server - DMS - Jan 99 - by Sebastien DENEUX */ defcom CeditionUser = editionUser S;; defcom CeditionAdmin = editionAdmin;; defcom CenvoiInventaireClient = envoiInventaireClient S ;; defcom CsupprimeElementRetour = supprimeElementRetour I;; defcom CmajElementRetour = majElementRetour I ;; defcom CajouteElementRetour = ajouteElementRetour I;; typeof db = SqlDB;; typeof dbname=S;; typeof dblogin=S;; typeof dbpassw=S;; typeof dbsql=S;; typeof listeAdmin = [CLIENT r1];; fun myDMSgetLogin (cli) = let _DMSgetLogin cli -> loginName in let strfind "@" loginName 0 -> pos in if pos == nil then loginName else substr loginName 0 pos ;; /*-----------*/ fun cliDestroyed(cli)= set listeAdmin = remove_from_list listeAdmin cli; _DMSevent this cli "destroyed" nil nil;; /*-----------*/ /*add element from list l [[S r1] r1] in the licent inventory*/ fun ajouteItems(l,cli)= if l!=nil then let l -> [[premierElement suiteElements] queue] in let suiteElements -> [secondElement suiteElements2] in let suiteElements2 -> [troisiemeElement fin] in if fin!= nil then (_fooS "ITEMS : Error in ajouteItem : Invalid SQL parameters list\n"; 0) else let mkItem [premierElement secondElement atoi troisiemeElement nil] -> monItem in (_DMSaddItem cli monItem; ajouteItems queue cli) else 0;; /*-----------*/ /* This function allows to add item in a data base and list of items in memory for a customer. */ fun AddToDB(cli,param)= let myDMSgetLogin cli -> NameLoginClient in let strextr param -> l in let getInfo l "reference" -> reference in let getInfo l "name" -> name in let getInfo l "count" -> count in if reference!=nil && count!=nil then if NameLoginClient!=nil then if findList listeAdmin cli then /* Only an admin can do it */ if name!=nil then /* Add the item in DB */ (SqlRequest db "INSERT INTO tableItems (login, refItem, nameItem, countItem) VALUES (?, ?, ?, ?)" (SQL_CHAR NameLoginClient)::(SQL_CHAR reference)::(SQL_CHAR name)::(SQL_INTEGER count)::nil; if (SqlCod db)==SQL_SUCCESS then /* Add the item in the memory */ let mkItem [reference name atoi count nil] -> monItem in (_DMSaddItem cli monItem;1) else let SqlDescErr db -> [etat _ _ _] in if !strcmp etat "23000" then /* The recordset already exists, so modify it */ (SqlRequest db "UPDATE tableItems SET countItem=countItem+? WHERE (login=? AND refItem=?)" (SQL_CHAR count)::(SQL_CHAR NameLoginClient)::(SQL_CHAR reference)::nil; if (SqlCod db)==SQL_SUCCESS then /* Modify item in memory */ (let _DMSfindItem cli reference -> currentItem in set currentItem.countItem=currentItem.countItem+atoi count;1) else /* Put in log */ nil ) else /* Put in log */ nil ) else (SqlRequest db "UPDATE tableItems SET countItem=countItem+? WHERE (login=? AND refItem=?)" (SQL_CHAR count)::(SQL_CHAR NameLoginClient)::(SQL_CHAR reference)::nil; if (SqlCod db)==SQL_SUCCESS then 1 else /* Put in log */ nil) else /* Message ? */ nil else /* Message ? */ nil else nil;; /*-----------*/ /* This function allows to sub item in a data base and list of items in memory for a customer. */ fun SubToDB(cli,param)= let myDMSgetLogin cli -> NameLoginClient in let strextr param -> l in let getInfo l "reference" -> reference in let getInfo l "count" -> count in if reference!=nil && count!=nil then if NameLoginClient!=nil then if findList listeAdmin cli then /* Only an admin can do it */ let hd hd SqlRequest db "SELECT countItem FROM tableItems WHERE (login=? AND refItem=?)" (SQL_CHAR NameLoginClient)::(SQL_CHAR reference)::nil -> oldCount in if (SqlCod db)==SQL_SUCCESS then if ((atoi oldCount)-(atoi count))<=0 then /* The recorset exists and we can not decrement the "count" field */ (SqlRequest db "DELETE FROM tableItems WHERE login=? AND refItem=?" (SQL_CHAR NameLoginClient)::(SQL_CHAR reference)::nil; if (SqlCod db)==SQL_SUCCESS then /* Delete the item in memory also */ (_DMSsubItem cli reference 1;1) else nil) else /* The recorset exists and we can decrement the "count" field */ (SqlRequest db "UPDATE tableItems SET countItem=countItem-? WHERE (login=? AND refItem=?)" (SQL_CHAR count)::(SQL_CHAR NameLoginClient)::(SQL_CHAR reference)::nil; if (SqlCod db)==SQL_SUCCESS then /* Modify the item in memory also */ (let _DMSfindItem cli reference -> currentItem in set currentItem.countItem=currentItem.countItem-atoi count;1) else nil ) else /* Put in log */ nil else /* Message ? */ nil else /* Message ? */ nil else nil;; /*-----------*/ fun activate(from,cli,action,param,rep)= if !strcmp action "getItems" then let myDMSgetLogin cli -> NomLoginClient in let SqlRequest db dbsql (SQL_CHAR NomLoginClient)::nil -> RESULT in if (SqlCod db)==SQL_SUCCESS then (_DMSclearItem cli; ajouteItems RESULT cli;0) else (_DMSclearItem cli;0) else if !strcmp action "editUser" then (let SqlRequest db dbsql (SQL_CHAR myDMSgetLogin cli)::nil -> RESULT in if (SqlCod db)==SQL_SUCCESS then (_DMSsend this cli CeditionUser [zip strbuild RESULT];0) else 0) else if !strcmp action "editAdmin" then (_DMSsend this cli CeditionAdmin []; set listeAdmin = cli::remove_from_list listeAdmin cli;0) else if !strcmp action "destroy" then (_DMSdelClientDMI this cli; cliDestroyed cli;0) else if !strcmp action "start" then _DMScreateClientDMI this cli nil else if !strcmp action "add" then AddToDB cli param else if !strcmp action "sub" then SubToDB cli param else nil;; /*-----------*/ fun replace_in_list (list, old, new)= if list==nil then nil else let list -> [first next] in (if !strcmp first old then new::next else first::replace_in_list next old new);; /*-----------*/ fun gererEspace(l)= if l!=nil then let l -> [tete queue] in let replace_in_list tete "" "²¤²" -> tete in tete::gererEspace queue else l;; /*-----------*/ /*send the inventory of client loginCLient to an admin client (editAdmin)*/ fun __demandeInventaireClient(loginClient)= if findList listeAdmin DMSsender then let SqlRequest db dbsql (SQL_CHAR loginClient)::nil -> RESULT in _DMSsend this DMSsender CenvoiInventaireClient [zip strbuild gererEspace RESULT] else nil;; /*-----------*/ /*dans le cas d'un client user le paramètre SnomLogin vaut nil*/ /*these 3 functions can be run either by a client user or a client admin*/ /*in the client admin case, it takes the login value an a verification is done to see that it is an admin client that has started the function*/ fun __ajouteElement(Sedit1, Sedit2, Sedit3, SnomLogin)= if SnomLogin!=nil then if findList listeAdmin DMSsender then (SqlRequest db "INSERT INTO tableItems (login, refItem, nameItem, countItem) VALUES (?, ?, ?, ?)" (SQL_CHAR SnomLogin)::(SQL_CHAR Sedit1)::(SQL_CHAR Sedit2)::(SQL_INTEGER Sedit3)::nil; if (SqlCod db)==SQL_SUCCESS then _DMSsend this DMSsender CajouteElementRetour [1] else _DMSsend this DMSsender CajouteElementRetour [0]) else nil else (SqlRequest db "INSERT INTO tableItems (login, refItem, nameItem, countItem) VALUES (?, ?, ?, ?)" (SQL_CHAR myDMSgetLogin DMSsender)::(SQL_CHAR Sedit1)::(SQL_CHAR Sedit2)::(SQL_INTEGER Sedit3)::nil; let SqlCod db -> flag in if flag==SQL_SUCCESS then _DMSsend this DMSsender CajouteElementRetour [1] else _DMSsend this DMSsender CajouteElementRetour [0]);; /*-----------*/ fun __supprimeElement(a, SnomLogin)= if SnomLogin!=nil then if findList listeAdmin DMSsender then (SqlRequest db "DELETE FROM tableItems WHERE login = ? and refItem = ?" (SQL_CHAR SnomLogin)::(SQL_CHAR a)::nil; let SqlCod db -> flag in if flag==SQL_SUCCESS then _DMSsend this DMSsender CsupprimeElementRetour [1] else _DMSsend this DMSsender CsupprimeElementRetour [0]) else nil else (SqlRequest db "DELETE FROM tableItems WHERE login = ? and refItem = ?" (SQL_CHAR myDMSgetLogin DMSsender)::(SQL_CHAR a)::nil; let SqlCod db -> flag in if flag==SQL_SUCCESS then _DMSsend this DMSsender CsupprimeElementRetour [1] else _DMSsend this DMSsender CsupprimeElementRetour [0]);; /*-----------*/ fun __majElement(Sedit2, Sedit3, Selement, SnomLogin)= if SnomLogin!=nil then if findList listeAdmin DMSsender then (SqlRequest db "UPDATE tableItems SET nameItem = ?, countItem = ? WHERE login = ? AND refItem = ?" (SQL_CHAR Sedit2)::(SQL_INTEGER Sedit3)::(SQL_CHAR SnomLogin)::(SQL_CHAR Selement)::nil; if (SqlCod db)==SQL_SUCCESS then _DMSsend this DMSsender CmajElementRetour [1] else _DMSsend this DMSsender CmajElementRetour [0]) else nil else (SqlRequest db "UPDATE tableItems SET nameItem = ?, countItem = ? WHERE login = ? AND refItem = ?" (SQL_CHAR Sedit2)::(SQL_INTEGER Sedit3)::(SQL_CHAR myDMSgetLogin DMSsender)::(SQL_CHAR Selement)::nil; if (SqlCod db)==SQL_SUCCESS then _DMSsend this DMSsender CmajElementRetour [1] else _DMSsend this DMSsender CmajElementRetour [0]);; /*-----------*/ fun IniDMI(file)= _DMSregisterDMI this @activate @cliDestroyed nil nil; let strextr _getpack _checkpack file -> l in (set dbname=getInfo l "dbname"; set dblogin=getInfo l "dblogin"; set dbpassw=getInfo l "dbpassw"; set dbsql=getInfo l "dbsql"); set db=SqlCreate _channel dbname dblogin if dbpassw==nil then "" else dbpassw; if db==nil then let strcat _DMSgetName this " : unable to access to dataBase" -> errorMsg in (_fooS strcat "### " errorMsg;_DLGMessageBox _channel nil "Error" errorMsg 0;0) else nil;;