Java tutorial
package com.webbfontaine.valuewebb.navigation; import com.webbfontaine.valuewebb.model.SimpleDocument; import com.webbfontaine.valuewebb.model.constants.Constants; import com.webbfontaine.valuewebb.model.util.Utils; import com.webbfontaine.valuewebb.validation.InfoHandling; import org.apache.commons.lang3.StringUtils; import org.jboss.seam.ScopeType; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.Scope; import org.jboss.seam.annotations.intercept.BypassInterceptors; import org.jboss.seam.bpm.Actor; import org.jboss.seam.core.Conversation; import org.jboss.seam.core.ConversationEntries; import org.jboss.seam.core.ConversationEntry; import org.jboss.seam.log.Log; import org.jboss.seam.log.Logging; import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import java.util.TreeSet; /** * Copyrights 2002-2010 Webb Fontaine * This software is the proprietary information of Webb Fontaine. * Its use is subject to License terms. * User: nigiyan * Date: Aug 5, 2010 */ /** * This class is responsible for managing opened documents, like redirect to alredy opened document if requested for it again. * Opened documents - retrieved for modification, are conversations. * The aim of this class is not a navigation thru pages but Document switching and navigation among them. * <p/> * Some notes:<br/> * Oldest conversation - the conversation which accessed time is less than others'.<br/> */ @Name("retrievedDocuments") @Scope(ScopeType.SESSION) @BypassInterceptors public class RetrievedDocuments implements Serializable { private static final long serialVersionUID = 1L; private static Log log = Logging.getLog(RetrievedDocuments.class); /** * Redirects to requested conversation id * * @param conversationId requested conversation id * @return true if successfully redirected */ public boolean redirectTo(String conversationId) { if (conversationId != null) { ConversationEntry conversationEntry = ConversationEntries.instance() .getConversationEntry(conversationId); if (conversationEntry.isDisplayable()) { conversationEntry.select(); InfoHandling.getInstance().setInfoList(Utils.translate("Document is already open.")); return true; } } return false; } /** * tries to find conversation id for given doc keys, if found resirects to opened doc/conversation * * @param docName constant from <code>Constants</code> * @param docId document id * @return true if document was opened previously and redirection is done successfully */ public boolean redirectTo(String docName, Object docId) { String idByDescription = getConversationIdByDescription(docName, docId); return redirectTo(idByDescription); } /** * @param docName name of document, see <code>Constants</code> class * @param docId id of document * @return conversation id of conversation where requested document is opened, * null if no conversation exists for requested document. * @see com.webbfontaine.valuewebb.model.constants.Constants */ public String getConversationIdByDescription(String docName, Object docId) { String description = constructDescription(docName, docId); Collection<ConversationEntry> entries = ConversationEntries.instance().getConversationEntries(); for (ConversationEntry entry : entries) { if (entry.isDisplayable() && entry.getDescription().equals(description)) { return entry.getId(); } } return null; } private ArrayList<String> getConversationsStartedWithDescription(String string) { ArrayList<String> convs = new ArrayList<String>(); Collection<ConversationEntry> entries = ConversationEntries.instance().getConversationEntries(); for (ConversationEntry entry : entries) { if (entry.isDisplayable() && entry.getDescription().startsWith(string)) { convs.add(entry.getId()); } } return convs; } /** * Method checks number of active conversations and if limit is exceeded ends conversation with smallest access time (old one). * * @see #isLimitExceeded() */ public void controlLimit() { if (isLimitExceeded()) { log.debug("Number of opened documents for user '{0}' reached maxumum, ending first opened", Actor.instance().getId()); order().last().end(); } } /** * Method checks number of active conversations and if limit is exceeded ends conversation with smallest access time (old one). * * @return true if exceeded */ public boolean isLimitExceeded() { Collection<ConversationEntry> entries = ConversationEntries.instance().getConversationEntries(); return entries != null && entries.size() >= Constants.MAX_OPENED_DOCUMENTS; } /** * Orders conversations, the last element is the oldest. * * @return ordered Set where last element is oldest conversation * @see org.jboss.seam.faces.Switcher#createSelectItems() */ public TreeSet<ConversationEntry> order() { ConversationEntries conversationEntries = ConversationEntries.getInstance(); if (conversationEntries == null) { return null; } TreeSet<ConversationEntry> orderedEntries = new TreeSet<ConversationEntry>(); orderedEntries.addAll(conversationEntries.getConversationEntries()); return orderedEntries; } public void close(String docName, Object docId) { String idByDescription = getConversationIdByDescription(docName, docId); if (idByDescription != null) { ConversationEntries.instance().getConversationEntry(idByDescription).end(); } else { log.warn("Trying to close {0} with id {1} while it has been already closed", docName, docId); } } /** * use only for existed docs */ private String constructDescription(String docName, Object docId) { return Utils.translate(docName) + ": " + docId; } /** * @param document need to get id and name to construct conversation description and if status (id) is null * description is constructed by adding "New <current number>" to doc name. * @return conversation description */ public String constructDescription(SimpleDocument document) { if (!StringUtils.isEmpty(Conversation.instance().getDescription())) { return Conversation.instance().getDescription(); } if (document.getId() == null) { //check for existed new docs ArrayList<String> convs = getConversationsStartedWithDescription(document.description() + ": New"); return Utils.translate(document.description()) + ": " + Utils.translate("New") + "#" + (convs.size() + 1); } else { return constructDescription(document.description(), document.getId()); } } }