Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RESSUP-1356] Add Sort ID to Custom Attribute Document #1605

Open
wants to merge 2 commits into
base: contrib
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- RESSUP-1356

ALTER TABLE CUSTOM_ATTRIBUTE_DOCUMENT ADD SORT_ID NUMBER(12,0);


Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.kuali.coeus.common.framework.custom;

import java.util.Comparator;

import org.kuali.coeus.common.framework.custom.attr.CustomAttributeDocument;

/** Add Sort ID to Custom Attribute Document Maintenance Document */
/**
*
* 1. If SORT_IDs for all the custom data fields have been defined, then display them based on the
* SORT_ID in ascending order from the top to the bottom under the Full Group Name: *** sub panel
*
* 2. If none of the custom data fields has SORT_ID defined, then display them alphabetically
*
* 3. If some of the custom data fields have SORT_ID defined while the rest do not, then display all of the fields with a SORT_ID first,
* organized by sort ID. Then list the ones without a SORT_ID in alphabetical order.
*
*/
public class CustomAttributeSortIdComparator implements Comparator<CustomAttributeDocument> {

private static final int MAX_CUSTOM_DATA_SORT_ID = 999999;

public int compare(CustomAttributeDocument cad1, CustomAttributeDocument cad2) {
try {
String label1 = cad1.getCustomAttribute().getLabel();
String label2 = cad2.getCustomAttribute().getLabel();
if (label1 == null) {
label1 = "";
}
if (label2 == null) {
label2 = "";
}

if(cad1.getSortId() == null) {
cad1.setSortId(new Integer(MAX_CUSTOM_DATA_SORT_ID));
}
if(cad2.getSortId() == null) {
cad2.setSortId(new Integer(MAX_CUSTOM_DATA_SORT_ID));
}

int sortIdComp = cad1.getSortId().compareTo(cad2.getSortId());
return ((sortIdComp == 0) ? label1.compareTo(label2) : sortIdComp);
}
catch (Exception e) {
return 0;
}
}
}

Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*
* Kuali Coeus, a comprehensive research administration system for higher education.
*
*
* Copyright 2005-2015 Kuali, Inc.
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
Expand All @@ -24,6 +24,7 @@
import org.kuali.coeus.common.framework.auth.task.TaskAuthorizationService;
import org.kuali.coeus.sys.framework.service.KcServiceLocator;
import org.kuali.rice.krad.util.GlobalVariables;
import org.kuali.coeus.common.framework.custom.CustomAttributeSortIdComparator;

import java.io.Serializable;
import java.util.*;
Expand All @@ -33,24 +34,24 @@
* The CustomDataHelperBase is the base class for all Custom Data Helper classes.
*/
public abstract class CustomDataHelperBase<T extends DocumentCustomData> implements Serializable {

private SortedMap<String, List> customAttributeGroups = new TreeMap<String, List>();

/*
* Is the end-user allowed to modify the custom data values?
*/
private boolean modifyCustomData = false;


protected abstract T getNewCustomData();

public abstract boolean documentNotRouted();
public abstract boolean documentNotRouted();

public abstract List<T> getCustomDataList();

public abstract Map<String, CustomAttributeDocument> getCustomAttributeDocuments();


/**
* This method builds the custom data collections used on the form and populates the values from the collection of AwardCustomData on the Award.
* @param customAttributeGroups
Expand All @@ -59,7 +60,7 @@ public abstract class CustomDataHelperBase<T extends DocumentCustomData> impleme
public void buildCustomDataCollectionsOnExistingDocument(SortedMap<String, List> customAttributeGroups) {
boolean documentNotRouted = false;
documentNotRouted = documentNotRouted();

/*
* Going through all customDataDocs and adding the custom ones already in the document
*/
Expand All @@ -74,9 +75,9 @@ public void buildCustomDataCollectionsOnExistingDocument(SortedMap<String, List>
}
}
}

/*
* Go through all the custom data documents and if the document HAS NOT ROUTED and there are new custom data documents available,
* Go through all the custom data documents and if the document HAS NOT ROUTED and there are new custom data documents available,
* add those to the document as well.
*/
if (documentNotRouted) {
Expand All @@ -97,9 +98,9 @@ protected void addToCustomDataList(Map.Entry<String, CustomAttributeDocument> cu
newCustomData.setCustomAttribute(customAttributeDocumentEntry.getValue().getCustomAttribute());
newCustomData.setCustomAttributeId(customAttributeDocumentEntry.getValue().getId().longValue());
newCustomData.setValue(customAttributeDocumentEntry.getValue().getCustomAttribute().getDefaultValue());
getCustomDataList().add(newCustomData);
getCustomDataList().add(newCustomData);
}

protected boolean alreadyExists(List<CustomAttributeDocument> entriesInCurrentGroup, Entry<String, CustomAttributeDocument> customAttributeDocumentEntry) {
for ( CustomAttributeDocument entry : entriesInCurrentGroup) {
if (entry.getId() == Integer.parseInt(customAttributeDocumentEntry.getKey())) {
Expand All @@ -110,13 +111,13 @@ protected boolean alreadyExists(List<CustomAttributeDocument> entriesInCurrentGr
}

protected void addToGroup(String groupName, SortedMap<String, List> customAttributeGroups, Entry<String, CustomAttributeDocument> customAttributeDocument) {
List<CustomAttributeDocument> customAttributeDocumentList = customAttributeGroups.get(groupName);
if (customAttributeDocumentList == null) {
customAttributeDocumentList = new ArrayList<CustomAttributeDocument>();
customAttributeGroups.put(groupName, customAttributeDocumentList);
}
customAttributeDocumentList.add(customAttributeDocument.getValue());
List<CustomAttributeDocument> customAttributeDocumentList = customAttributeGroups.get(groupName);
if (customAttributeDocumentList == null) {
customAttributeDocumentList = new ArrayList<CustomAttributeDocument>();
customAttributeGroups.put(groupName, customAttributeDocumentList);
}
customAttributeDocumentList.add(customAttributeDocument.getValue());
}

protected boolean isMatch(T documentCustomData, Entry<String, CustomAttributeDocument> customAttributeDocumentEntry) {
return documentCustomData.getCustomAttributeId() == ((long)customAttributeDocumentEntry.getValue().getId());
Expand All @@ -130,24 +131,23 @@ protected boolean isMatch(T documentCustomData, Entry<String, CustomAttributeDoc
public void buildCustomDataCollectionsOnNewDocument(SortedMap<String, List> customAttributeGroups) {
for(Map.Entry<String, CustomAttributeDocument> customAttributeDocumentEntry:getCustomAttributeDocuments().entrySet()) {
String groupName = customAttributeDocumentEntry.getValue().getCustomAttribute().getGroupName();

addToCustomDataList(customAttributeDocumentEntry);

if (StringUtils.isEmpty(groupName)) {
groupName = "No Group";
}

List<CustomAttributeDocument> customAttributeDocumentList = customAttributeGroups.get(groupName);
if (customAttributeDocumentList == null) {
customAttributeDocumentList = new ArrayList<CustomAttributeDocument>();
customAttributeGroups.put(groupName, customAttributeDocumentList);

}
customAttributeDocumentList.add(getCustomAttributeDocuments().get(customAttributeDocumentEntry.getValue().getId().toString()));
Collections.sort(customAttributeDocumentList, new LabelComparator());
}
}

public void prepareCustomData() {
initializePermissions();
SortedMap<String, List> customAttributeGroups = new TreeMap<String, List>();
Expand All @@ -157,10 +157,32 @@ public void prepareCustomData() {
buildCustomDataCollectionsOnNewDocument(customAttributeGroups);
}
setCustomAttributeGroups(customAttributeGroups);
sortCustomDataBySortId();
}


protected void sortCustomDataBySortId() {
CustomAttributeSortIdComparator customAttributeDocumentComparator = new CustomAttributeSortIdComparator();
List<T> unsortedCustomDataList = new ArrayList<>(getCustomDataList());
List<CustomAttributeDocument> sortedCustomAttributeDocuments = new ArrayList<>(getCustomAttributeDocuments().values());
Collections.sort(sortedCustomAttributeDocuments, customAttributeDocumentComparator);
getCustomDataList().clear();

for (CustomAttributeDocument customAttributeDocument : sortedCustomAttributeDocuments) {
for (T customData : unsortedCustomDataList) {
if (customData.getCustomAttributeId().equals(customAttributeDocument.getId())) {
getCustomDataList().add(customData);
break;
}
}
}

for (List<CustomAttributeDocument> groupDocuments : getCustomAttributeGroups().values()) {
Collections.sort(groupDocuments, customAttributeDocumentComparator);
}
}

/**
*
*
* This method takes in a groupName from the data entry and return a valid string to use in the Map functions later.
* Note, data entry may create a null group name, which is invalid with the Map funcitons.
* @param groupName
Expand All @@ -169,30 +191,30 @@ public void prepareCustomData() {
public String getValidCustomAttributeGroupName(String groupName) {
return groupName != null ? groupName : "Custom Data Group";
}

/**
* Initialize the permissions for viewing/editing the Custom Data web page.
*/
public void initializePermissions() {
modifyCustomData = canModifyCustomData();
}

/**
* Can the current user modify the custom data values?
* @return true if can modify the custom data; otherwise false
*/
public boolean canModifyCustomData() {
return modifyCustomData;
}

/**
* Get the ModifyCustomData value.
* @return the ModifyCustomData value
*/
public boolean getModifyCustomData() {
return modifyCustomData;
}

/**
* Sets the customAttributeGroups attribute value.
* @param customAttributeGroups The customAttributeGroups to set.
Expand All @@ -208,7 +230,7 @@ public void setCustomAttributeGroups(SortedMap<String, List> customAttributeGrou
public Map<String, List> getCustomAttributeGroups() {
return customAttributeGroups;
}

protected TaskAuthorizationService getTaskAuthorizationService() {
return KcServiceLocator.getService(TaskAuthorizationService.class);
}
Expand All @@ -218,29 +240,29 @@ protected TaskAuthorizationService getTaskAuthorizationService() {
* @return the current session's userName
*/
protected String getUserIdentifier() {
return GlobalVariables.getUserSession().getPrincipalId();
return GlobalVariables.getUserSession().getPrincipalId();
}

/**
* Set the custom attribute content in workflow.
*/
public void setCustomAttributeContent(String documentNumber, String attributeName) {
getCustomAttributeService().setCustomAttributeKeyValue(documentNumber, getCustomAttributeDocuments(), attributeName, getUserIdentifier());
}
protected CustomAttributeService getCustomAttributeService() {
return KcServiceLocator.getService(CustomAttributeService.class);
}
public void setCustomAttributeContent(String documentNumber, String attributeName) {
getCustomAttributeService().setCustomAttributeKeyValue(documentNumber, getCustomAttributeDocuments(), attributeName, getUserIdentifier());
}

protected CustomAttributeService getCustomAttributeService() {
return KcServiceLocator.getService(CustomAttributeService.class);
}

/**
* Sorts custom data attributes by label for alphabetical order on custom data panels.
*/
public class LabelComparator implements Comparator
{
{
public LabelComparator(){}

public int compare(Object cad1, Object cad2 )
{
{
try
{
String label1 = ((CustomAttributeDocument)cad1).getCustomAttribute().getLabel();
Expand All @@ -253,7 +275,7 @@ public int compare(Object cad1, Object cad2 )
{
label2 = "";
}
return label1.compareTo(label2);
return label1.compareTo(label2);
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*
* Kuali Coeus, a comprehensive research administration system for higher education.
*
*
* Copyright 2005-2015 Kuali, Inc.
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
Expand All @@ -24,7 +24,7 @@
import org.kuali.rice.kew.doctype.bo.DocumentType;

/**
*
*
* This class bo of CustomAttributeDocument.
*/
public class CustomAttributeDocument extends KcPersistableBusinessObjectBase implements MutableInactivatable, CustomAttributeDocumentContract {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should extend KraSortablePersistableBusinessObjectBase

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Travis,

The changes have been pushed to RESSUP-1356.

Thanks,
Chao

From: Travis Schneeberger <[email protected]mailto:[email protected]>
Reply-To: kuali/kc <[email protected]mailto:[email protected]>
Date: Monday, March 14, 2016 1:24 PM
To: kuali/kc <[email protected]mailto:[email protected]>
Cc: Chao Yue <[email protected]mailto:[email protected]>
Subject: Re: [kc] [RESSUP-1356] Add Sort ID to Custom Attribute Document (#1605)

In coeus-impl/src/main/java/org/kuali/coeus/common/framework/custom/attr/CustomAttributeDocument.javahttps://github.com//pull/1605#discussion_r56039542:

@@ -24,7 +24,7 @@
import org.kuali.rice.kew.doctype.bo.DocumentType;

/**

    • This class bo of CustomAttributeDocument.
      */
      public class CustomAttributeDocument extends KcPersistableBusinessObjectBase implements MutableInactivatable, CustomAttributeDocumentContract {

should extend KraSortablePersistableBusinessObjectBase

Reply to this email directly or view it on GitHubhttps://github.com//pull/1605/files#r56039542.

Expand All @@ -43,6 +43,8 @@ public class CustomAttributeDocument extends KcPersistableBusinessObjectBase imp

private boolean active;

private Integer sortId;

public CustomAttributeDocument() {
super();
}
Expand Down Expand Up @@ -108,4 +110,8 @@ public DocumentType getDocumentType() {
public void setDocumentType(DocumentType documentType) {
this.documentType = documentType;
}

public Integer getSortId() { return sortId; }

public void setSortId(Integer sortId) { this.sortId = sortId; }
}
Loading