I am using a combination of the following two classes to achieve a quite simple task. I have Users in my application. These Users can be assigned Roles. And these Roles need to be assigned Rights.
Currently I do this with a primefaces selectCheckboxMenu
. This menu requires the entries to be Strings. In itself this is not really hard. But the fun just begins. I need to assign the selected entries back to the processed Role before passing it to the businessInterface.save()
method for persistence.
That's why I wrote a ModelConverter
class to provide a static method doing that conversion for me. This is mainly due to the fact that I am limited to Java 6 in the project. I'd love to go for Java 7 or even 8, but oh well ;)
But when I look at the usage of that class it looks ugly, and I'd like to change that.
RoleEditController.java:
import static company.crm.controller.ControllerSettings.CONVERSATION_STANDARD_TIMEOUT;
import company.crm.business.RoleDistributor;
import company.crm.controller.Mode;
import company.crm.controller.Pages;
import company.crm.framework.ModelConverter;
import company.crm.model.Right;
import company.crm.model.Role;
@Named
@ConversationScoped
public class RoleEditController implements Serializable {
private static final long serialVersionUID = 4002592161426207130L;
@Inject
private RoleListController roleListController;
@Inject
private RoleDistributor businessInterface;
@Inject
private Conversation conversation;
private Mode mode;
private Role currentRole;
private static List<Right> allRights = new ArrayList<Right>();
private List<String> selectedRights = new ArrayList<String>();
@PostConstruct
public void init() {
conversation.setTimeout(CONVERSATION_STANDARD_TIMEOUT);
conversation.begin();
this.mode = Mode.ADD;
reloadAllRightsList();
}
public String save() {
currentRole.setRights(new ArrayList<Right>(ModelConverter
.objectifyStringifiedCollectionWithOriginalItems(allRights,
selectedRights)));
boolean success = businessInterface.save(mode, currentRole);
roleListController.fillRoleList();
if (success) {
conversation.end();
return Pages.ROLE_LIST;
} else {
return Pages.ROLE_EDIT;
}
}
public void reloadCurrentRole() {
reloadAllRightsList();
this.currentRole = businessInterface.loadSingle(currentRole.getId());
selectedRights = new ArrayList<String>(
ModelConverter.stringifyCollection(currentRole.getRights()));
}
private void reloadAllRightsList() {
allRights.clear();
allRights.addAll(businessInterface.loadPossibleRights());
}
public String abort() {
conversation.end();
return Pages.ROLE_LIST;
}
public void recieveRole(final long roleId) {
currentRole = businessInterface.loadSingle(roleId);
selectedRights = new ArrayList<String>(
ModelConverter.stringifyCollection(currentRole.getRights()));
this.mode = roleId == 0 ? Mode.ADD : Mode.EDIT;
}
public Role getCurrentRole() {
return currentRole;
}
public Mode getMode() {
return mode;
}
public List<String> getStringifiedRightList() {
return new ArrayList<String>(
ModelConverter.stringifyCollection(allRights));
}
public List<String> getSelectedRights() {
return selectedRights;
}
public void setSelectedRights(final List<String> selectedRights) {
this.selectedRights = selectedRights;
}
}
ModelConverter.java:
package novatec.crm.framework;
import java.util.Collection;
import java.util.HashSet;
public final class ModelConverter {
public static Collection<String> stringifyCollection(
final Collection<? extends Object> items) {
if (items == null) {
return new HashSet<String>();
}
Collection<String> stringifiedCollection = new HashSet<String>();
for (Object item : items) {
stringifiedCollection.add(item.toString());
}
return stringifiedCollection;
}
public static <T> Collection<T> objectifyStringifiedCollectionWithOriginalItems(
final Collection<? extends T> originalItems,
final Collection<String> stringifiedItems) {
if (originalItems.size() < stringifiedItems.size()) {
throw new IllegalArgumentException(
"The original collection may not be smaller than the subcollection of stringified Items");
}
Collection<T> convertedItems = new HashSet<T>();
for (T item : originalItems) {
for (String stringifiedItem : stringifiedItems) {
if (item.toString().equals(stringifiedItem)) {
convertedItems.add(item);
}
}
}
return convertedItems;
}
}
Questions:
- Am I overly complicating things?
- Is this another case of German overengineering™?
- Are my names telling the intention behind my code?
And before anyone calls it: I know that the raw string error message doesn't look nice, and that my Converter does automagically remove duplicates in the Collections that are passed to him.
HashSet<String>
to the top of the method and then check if the collection parameter ! null (talking about the converter class) (from my phone) \$\endgroup\$