Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {includes/dsUserUserRole.i}
- PROCEDURE submitUsers:
- DEFINE INPUT PARAMETER DATASET FOR dsUserUserRole.
- DEFINE VARIABLE iInitialUserId AS INTEGER NO-UNDO.
- DEFINE VARIABLE returnError AS CHARACTER NO-UNDO.
- DEFINE VARIABLE iAuxUserId AS INTEGER NO-UNDO.
- /* Delete */
- RUN deleteUserRole(DATASET dsUserUserRole).
- IF RETURN-VALUE <> '' THEN
- DO:
- returnError = returnError + CHR(10) + RETURN-VALUE.
- END.
- RUN deleteAppUsers (DATASET dsUserUserRole).
- IF RETURN-VALUE <> '' THEN
- DO:
- returnError = returnError + CHR(10) + RETURN-VALUE.
- END.
- FOR EACH ttAppUser
- WHERE NOT ROW-STATE(ttAppUser) EQ ROW-UNMODIFIED:
- CASE ROW-STATE(ttAppUser):
- /* Update */
- WHEN 2 THEN
- DO:
- RUN updateUser(BUFFER ttAppUser).
- IF RETURN-VALUE <> '' THEN
- DO:
- returnError = returnError + CHR(10) + RETURN-VALUE.
- END.
- END.
- /* Create*/
- WHEN 3 THEN
- DO:
- /* saves the id of the parent record that came from the GUI <iInitialUserId> */
- iInitialUserId = ttAppUser.AppUserId.
- /* holds the actual id with which the record was added to the db <iAuxUserId> */
- RUN createUser (BUFFER ttAppUser, INPUT-OUTPUT iAuxUserId).
- DELETE ttAppUser.
- IF RETURN-VALUE <> '' THEN
- DO:
- returnError = returnError + CHR(10) + RETURN-VALUE.
- END.
- /* finds all the roles of the added user using the the Id that came from the GUI <iInitialUserId> */
- FOR EACH ttUserRole EXCLUSIVE-LOCK
- WHERE ttUserRole.AppUserId = iInitialUserId:
- /* adds the roles to the db with the actual current Id of the parent record */
- RUN createuserRole(BUFFER ttUSerRole, INPUT iAuxUserId).
- DELETE ttUserRole.
- END.
- END.
- END CASE.
- END.
- FOR EACH ttUserRole
- WHERE NOT ROW-STATE(ttUserRole) EQ ROW-UNMODIFIED:
- CASE ROW-STATE(ttUserRole):
- WHEN 2 THEN
- DO:
- RUN updateUserRole(BUFFER ttUserRole).
- IF RETURN-VALUE <> '' THEN
- DO:
- returnError = returnError + CHR(10) + RETURN-VALUE.
- END.
- END.
- WHEN 3 THEN
- DO:
- RUN createUserRole(BUFFER ttUserRole, ttUserRole.AppUserId).
- IF RETURN-VALUE <> '' THEN
- DO:
- returnError = returnError + CHR(10) + RETURN-VALUE.
- END.
- END.
- END CASE.
- END.
- RETURN returnError.
- END PROCEDURE.
- /* ------------------------------User------------------------------- */
- /* -----------------------------Validations--------------------------------*/
- /* ------------------------------UserCreate------------------------------- */
- PROCEDURE createUser:
- DEFINE PARAMETER BUFFER ttAppUser FOR TEMP-TABLE ttAppUser.
- DEFINE INPUT-OUTPUT PARAMETER iFinalUserId AS INTEGER NO-UNDO.
- DEFINE VARIABLE iAuxAppUserId AS INTEGER NO-UNDO.
- DEFINE VARIABLE iAuxUserRoleId AS INTEGER NO-UNDO.
- FIND FIRST AppUser
- WHERE AppUser.Username = ttAppUser.Username NO-LOCK NO-ERROR.
- /*Check if username exists */
- IF AVAILABLE AppUser THEN
- RETURN 'Create new User Error: Username already exists'.
- FOR LAST AppUser NO-LOCK:
- IF AVAILABLE AppUser THEN
- ttAppUser.AppUserId = AppUser.AppUserId + 1.
- ELSE
- ttAppUser.AppUserId = 1.
- END.
- /* Set User info*/
- CREATE AppUser.
- ASSIGN
- AppUser.AppUserId = ttAppUser.AppUserId
- iFinalUserId = ttAppUser.AppUserId
- AppUser.NAME = TRIM(ttAppUser.NAME)
- AppUser.UserName = TRIM(ttAppUser.UserName)
- AppUser.Password = TRIM(ttAppUser.password).
- RETURN.
- END PROCEDURE.
- /* ------------------------------UserRead------------------------------- */
- /*----------------------------Filter Users ---------------------------- */
- PROCEDURE filterUsers:
- DEFINE OUTPUT PARAMETER DATASET FOR dsUserUserRole.
- DEFINE INPUT PARAMETER ipcFieldValue AS CHARACTER NO-UNDO.
- DEFINE INPUT PARAMETER ipcFieldName AS CHARACTER NO-UNDO.
- DEFINE VARIABLE queryStatement AS CHARACTER NO-UNDO.
- DEFINE VARIABLE hQuery AS HANDLE NO-UNDO.
- DEFINE VARIABLE hTableBuffer AS HANDLE NO-UNDO.
- DEFINE VARIABLE cQueryStatement AS CHARACTER NO-UNDO.
- DEFINE VARIABLE lResult AS LOGICAL NO-UNDO.
- DEFINE VARIABLE iNumEntries AS INTEGER NO-UNDO.
- DEFINE VARIABLE iX AS INTEGER NO-UNDO.
- iNumEntries = NUM-ENTRIES (ipcFieldName, ',').
- EMPTY TEMP-TABLE ttAppUser.
- EMPTY TEMP-TABLE ttUserRole.
- CREATE BUFFER hTableBuffer FOR TABLE "AppUser".
- CREATE QUERY hQuery.
- hQuery:SET-BUFFERS(hTableBuffer).
- cQueryStatement = 'For each AppUser where '.
- REPEAT ix = 1 TO iNumEntries - 1:
- cQueryStatement = cQueryStatement + SUBSTITUTE ('appUser.&1 Matches (~'*&2*~') or ', ENTRY(ix, ipcFieldName, ','), ipcFieldValue).
- END.
- cQueryStatement = cQueryStatement + SUBSTITUTE ('appUser.&1 Matches (~'*&2*~') .', ENTRY(iNumEntries, ipcFieldName, ','), ipcFieldValue).
- MESSAGE cQueryStatement
- VIEW-AS ALERT-BOX INFORMATION BUTTONS OK.
- lResult = hQuery:QUERY-PREPARE(cQueryStatement) NO-ERROR.
- IF lResult = FALSE THEN
- DO:
- MESSAGE SUBSTITUTE ("Query statement is invalid: &1",
- cQueryStatement)
- VIEW-AS ALERT-BOX INFORMATION BUTTONS OK.
- RETURN.
- END.
- hQuery:QUERY-OPEN.
- hQuery:GET-FIRST (NO-LOCK).
- DO WHILE NOT hQuery:QUERY-OFF-END:
- CREATE ttAppUser.
- ttAppUser.AppUserId = hTableBuffer:BUFFER-FIELD("appUserId"):BUFFER-VALUE.
- ttAppUser.NAME = hTableBuffer:BUFFER-FIELD("NAME"):BUFFER-VALUE.
- ttAppUser.UserName = hTableBuffer:BUFFER-FIELD("UserName"):BUFFER-VALUE.
- ttAppUser.Password = hTableBuffer:BUFFER-FIELD("Password"):BUFFER-VALUE.
- hQuery:GET-NEXT (NO-LOCK).
- END.
- FOR EACH ttAppUser NO-LOCK,
- EACH UserRole NO-LOCK
- WHERE UserRole.AppUserId = ttAppUser.AppUserID:
- CREATE ttUserRole.
- BUFFER-COPY UserRole TO ttUserRole.
- END.
- hQuery:QUERY-CLOSE.
- DELETE OBJECT hQuery.
- DELETE OBJECT hTableBuffer.
- END PROCEDURE.
- PROCEDURE getAllUsers:
- DEFINE OUTPUT PARAMETER DATASET FOR dsUserUserRole.
- DATASET dsUserUserRole:EMPTY-DATASET().
- FOR EACH AppUser NO-LOCK:
- CREATE ttAppUser.
- BUFFER-COPY AppUser TO ttAppUser.
- END.
- RETURN.
- END PROCEDURE.
- /* ------------------------------UserUpdate------------------------------- */
- PROCEDURE updateUsersPass:
- DEFINE PARAMETER BUFFER ttAppUser FOR TEMP-TABLE ttAppUser.
- FIND FIRST AppUser WHERE AppUser.AppUserId = ttAppUser.AppUserId NO-LOCK NO-ERROR.
- IF NOT AVAILABLE AppUser THEN
- DO:
- RETURN 'Update error: Cannot find User'.
- END.
- ASSIGN
- AppUser.Password = ttAppUser.Password.
- RETURN.
- END PROCEDURE.
- PROCEDURE updateUser:
- DEFINE PARAMETER BUFFER ttAppUser FOR TEMP-TABLE ttAppUser.
- FIND FIRST AppUser WHERE AppUser.AppUserId = ttAppUser.AppUserId NO-LOCK NO-ERROR.
- IF NOT AVAILABLE AppUser THEN
- DO:
- RETURN 'Update error: Cannot find User'.
- END.
- FIND FIRST appUser WHERE appUser.username = ttAppUser.username NO-LOCK NO-ERROR.
- IF NOT AVAILABLE AppUser THEN
- DO:
- ASSIGN
- AppUser.UserName = TRIM(ttAppUser.Username)
- AppUser.NAME = TRIM(ttAppUser.NAME)
- AppUser.Password = TRIM(ttAppUser.Password).
- END.
- ELSE DO:
- RETURN 'Update error: There already is an user with that Username'.
- END.
- RETURN.
- END PROCEDURE.
- /* ------------------------------UserDelete------------------------------- */
- PROCEDURE deleteAppUsers:
- DEFINE INPUT PARAMETER DATASET FOR dsUserUserRole.
- FOR EACH bttAppUser NO-LOCK
- WHERE ROW-STATE(bttAppUser) EQ ROW-DELETED:
- FIND FIRST AppUser EXCLUSIVE-LOCK WHERE AppUser.Username = bttAppUser.Username NO-ERROR.
- IF NOT AVAILABLE AppUser THEN
- DO:
- RETURN 'An error has occured please try again'.
- END.
- /*Check for active Roles*/
- FIND FIRST UserRole NO-LOCK
- WHERE UserRole.AppUserId = bttAppUser.AppUserId NO-ERROR.
- IF AVAILABLE UserRole THEN
- DO:
- RETURN 'User has active roles. Please delete them first'.
- END.
- ELSE DO:
- DELETE AppUser.
- END.
- END.
- RETURN.
- END PROCEDURE.
- /* ------------------------------User Role------------------------------- */
- /* ------------------------------Create UserRole------------------------------- */
- PROCEDURE createUserRole:
- DEFINE PARAMETER BUFFER bttUserRole FOR TEMP-TABLE ttUserRole.
- DEFINE INPUT PARAMETER iAppUserId AS INTEGER NO-UNDO.
- DEFINE VARIABLE iAuxUserRoleId AS INTEGER NO-UNDO.
- /*Check if the user already has that role for the store*/
- FIND FIRST Store NO-LOCK WHERE Store.StoreId = bttUserRole.StoreId NO-ERROR.
- IF NOT AVAILABLE Store THEN
- DO:
- RETURN 'Create failed: Could not find store'.
- END.
- FIND FIRST UserRole NO-LOCK WHERE UserRole.AppUserId = iAppUserId AND
- UserRole.RoleId = bttUserRole.RoleId AND
- UserRole.StoreId = bttUserRole.StoreId NO-ERROR.
- IF AVAILABLE UserRole THEN
- DO:
- RETURN 'User already has a role of that type for this store' .
- END.
- ELSE
- DO:
- FOR LAST UserRole EXCLUSIVE-LOCK:
- iAuxUserRoleId = UserRole.UserRoleId + 1.
- END.
- CREATE UserRole.
- BUFFER-COPY bttUserRole EXCEPT bttUserRole.UserRoleId bttUserRole.AppUSerId TO UserRole.
- UserRole.UserRoleId = iAuxUserRoleId.
- UserRole.AppUserId = iAppUserId.
- END.
- RETURN.
- END PROCEDURE.
- /* ------------------------------Read UserRole------------------------------- */
- PROCEDURE getAllUserRoles:
- DEFINE INPUT-OUTPUT PARAMETER DATASET FOR dsUserUserRole.
- EMPTY TEMP-TABLE ttUserRole.
- FOR EACH UserRole NO-LOCK:
- CREATE ttUserRole.
- BUFFER-COPY UserRole TO ttUserRole.
- END.
- RETURN.
- END PROCEDURE.
- PROCEDURE getAllUserRolesforUser:
- DEFINE INPUT-OUTPUT PARAMETER DATASET FOR dsUserUserRole.
- EMPTY TEMP-TABLE ttUserRole.
- FOR EACH ttAppUser NO-LOCK,
- EACH UserRole NO-LOCK
- WHERE UserRole.AppUserId = ttAppUser.AppUserID:
- CREATE ttUserRole.
- BUFFER-COPY UserRole TO ttUserRole.
- MESSAGE ttUserRole.UserRoleId
- VIEW-AS ALERT-BOX INFORMATION BUTTONS OK.
- END.
- RETURN.
- END PROCEDURE.
- /* ------------------------------Update UserRole------------------------------- */
- PROCEDURE updateUserRole:
- DEFINE PARAMETER BUFFER ttUserRole FOR TEMP-TABLE ttUserRole.
- FIND FIRST UserRole NO-LOCK WHERE UserRole.UserRoleId = ttUserRole.UserRoleId NO-ERROR.
- IF NOT AVAILABLE UserRole THEN
- DO:
- RETURN ERROR 'An error has occured please try again'.
- END.
- FIND FIRST Store NO-LOCK WHERE Store.StoreId = ttUserRole.StoreId NO-ERROR.
- IF NOT AVAILABLE Store THEN
- DO:
- MESSAGE 'In if not available'
- VIEW-AS ALERT-BOX INFORMATION BUTTONS OK.
- RETURN 'Update failed: Could not find store'.
- END.
- ELSE DO:
- BUFFER-COPY ttUserRole EXCEPT UserRoleId AppUserId TO UserRole .
- END.
- RETURN.
- END PROCEDURE.
- /* ------------------------------Delete UserRole------------------------------- */
- PROCEDURE deleteUserRole:
- DEFINE INPUT PARAMETER DATASET FOR dsUserUserRole.
- FOR EACH bttUserRole NO-LOCK
- WHERE ROW-STATE(bttUserRole) EQ ROW-DELETED:
- /*Check if the role was an admin type */
- IF bttUserRole.RoleId = 2 THEN
- DO:
- /* Check if there exists another manager for that store */
- FIND FIRST UserRole NO-LOCK WHERE UserRole.UserRoleId <> bttUserRole.UserRoleId AND
- UserRole.RoleId = 2 AND UserRole.StoreId = bttUserRole.StoreId NO-ERROR.
- IF NOT AVAILABLE UserRole THEN
- DO:
- RETURN-VALUE = 'Cannot delete manager, please add another manager for this store first'.
- END.
- /* If there exists another admin delete the role*/
- ELSE DO:
- FIND FIRST userRole EXCLUSIVE-LOCK
- WHERE UserRole.UserRoleId = bttUserRole.UserRoleId.
- IF NOT AVAILABLE UserRole THEN
- DO:
- RETURN ERROR 'An error has occured please try again'.
- END.
- DELETE UserRole.
- END.
- END.
- /*Case where the role is of customer type */
- ELSE DO:
- FIND FIRST userRole EXCLUSIVE-LOCK
- WHERE UserRole.UserRoleId = bttUserRole.UserRoleId NO-ERROR.
- IF NOT AVAILABLE UserRole THEN
- DO:
- RETURN ERROR 'An error has occured please try again'.
- END.
- DELETE UserRole.
- END.
- END.
- RETURN.
- END PROCEDURE.
- /* Checks for user roles that have no coresponding user and deletes them (since they are invalid) */
- PROCEDURE cleanseUserRoles:
- FOR EACH UserRole EXCLUSIVE-LOCK:
- FIND FIRST AppUser WHERE AppUser.AppUserId = UserRole.AppUserId NO-ERROR.
- IF NOT AVAILABLE AppUser THEN
- DO:
- DELETE UserRole.
- END.
- END.
- END PROCEDURE.
- /*--------------------------------------Tests-----------------------------*/
- /*------------------------------------Set-Up-------------------------------*/
- /* DEFINE VARIABLE Icount AS INTEGER NO-UNDO. */
- /* DO TRANSACTION: */
- /* TEMP-TABLE ttAppUser:TRACKING-CHANGES = TRUE. */
- /* TEMP-TABLE ttUserRole:TRACKING-CHANGES = TRUE. */
- /*---------------------------Create---------------------------------*/
- /* DO iCount = 90 TO 93: */
- /* CREATE ttAppUser. */
- /* ASSIGN ttAppUser.AppUserID = iCount */
- /* ttAppUser.NAME = SUBSTITUTE('Name&1', iCount) */
- /* ttAppUser.UserName = SUBSTITUTE('User&1', iCount) */
- /* ttAppUser.Password = SUBSTITUTE('password&1', iCount). */
- /* END. */
- /* */
- /* FOR EACH ttAppUser: */
- /* DISPLAY ttAppUser. */
- /* END. */
- /* */
- /* DO iCount = 90 TO 93: */
- /* CREATE ttUserRole. */
- /* ASSIGN */
- /* ttUserRole.UserRoleId = iCount */
- /* ttUserRole.AppUserId = iCount */
- /* ttUserRole.DESCRIPTION = 'Description' */
- /* ttUserRole.StoreId = iCount. */
- /* ttUserRole.RoleId = 3. */
- /* END. */
- /* */
- /* FOR EACH ttUserRole: */
- /* DISPLAY ttUserRole EXCEPT ttUserRole.DESCRIPTION. */
- /* END. */
- /* */
- /* */
- /* RUN submitUsers (INPUT DATASET dsUserUserRole). */
- /* */
- /* FOR EACH AppUser NO-LOCK: */
- /* DISPLAY AppUser.AppUserID AppUser.NAME FORMAT 'x(5)' AppUser.UserName FORMAT 'x(10)' AppUser.Password FORMAT 'x(10)'. */
- /* END. */
- /* */
- /* */
- /* FOR EACH UserRole NO-LOCK: */
- /* DISPLAY UserRole EXCEPT UserRole.DESCRIPTION. */
- /* END. */
- /* UNDO. */
- /* END. */
- /* */
- /*-------------------------Update----------------------------------*/
- /*-------------------------Delete----------------------------------*/
- /* iCount = 1. */
- /* RUN getAllUsers(OUTPUT DATASET dsUserUserRole). */
- /* DATASET dsUserUserRole:ACCEPT-CHANGES(). */
- /* FOR EACH ttAppUser EXCLUSIVE-LOCK */
- /* WHERE ttAppUser.AppUserId > 13: */
- /* DELETE ttAppUser. */
- /* IF iCount = 6 THEN */
- /* DO: */
- /* LEAVE. */
- /* END. */
- /* iCount = iCount + 1. */
- /* END. */
- /* */
- /* /* FOR EACH ttAppUser: */ */
- /* /* DISPLAY ttAppUser. */ */
- /* /* END. */ */
- /* */
- /* RUN submitUsers(INPUT DATASET dsUserUserRole). */
- /* */
- /* FOR EACH AppUser NO-LOCK: */
- /* DISPLAY AppUser.AppUserID AppUser.NAME FORMAT 'x(5)' AppUser.UserName FORMAT 'x(10)' AppUser.Password FORMAT 'x(10)'. */
- /* END. */
- /* */
- /* */
- /* */
- /* UNDO. */
- /* END. */
- /*--------------------------------Test Create-------------------------------*/
- /* FOR EACH AppUser WHERE AppUser.AppUserId > 80: */
- /* DELETE AppUser. */
- /* END. */
- /* */
- /* RUN createUsers (INPUT DATASET dsUserUserRole). */
- /* DATASET dsUserUserRole:ACCEPT-CHANGES(). */
- /* */
- /* */
- /* FOR EACH AppUser NO-LOCK, */
- /* EACH UserRole NO-LOCK */
- /* WHERE UserRole.AppUserId = AppUser.AppUserId AND AppUser.AppUserId > 14: */
- /* DELETE UserRole. */
- /* */
- /* */
- /* END. */
- /* */
- /* FOR EACH ttAppUser NO-LOCK: */
- /* DELETE ttAppUser. */
- /* END. */
- /* RUN deleteAppUser( INPUT DATASET dsUserUserRole). */
- /* */
- /* FOR EACH AppUser NO-LOCK: */
- /* DISPLAY AppUser.AppUserID AppUser.NAME FORMAT 'x(5)' AppUser.UserName FORMAT 'x(10)' AppUser.Password FORMAT 'x(10)'. */
- /* END. */
- /* UNDO. */
- /* END.
- */
- /* DO TRANSACTION: */
- /* CREATE ttUserRole. */
- /* FOR FIRST UserRole NO-LOCK: */
- /* BUFFER-COPY UserRole TO ttUserRole. */
- /* END. */
- /* */
- /* TEMP-TABLE ttUserRole:TRACKING-CHANGES = TRUE. */
- /* */
- /* DISPLAY ttUserRole EXCEPT DESCRIPTION. */
- /* */
- /* ttUserRole.StoreId = 1000. */
- /* */
- /* RUN submitUsers(DATASET dsUserUserRole) . */
- /* */
- /* */
- /* END. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement