/* * Site.java * Mar 10, 2003 */ package net.sourceforge.fenixedu.domain; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; import net.sourceforge.fenixedu.domain.accessControl.EveryoneGroup; import net.sourceforge.fenixedu.domain.accessControl.InternalPersonGroup; import net.sourceforge.fenixedu.domain.contents.Attachment; import net.sourceforge.fenixedu.domain.contents.Container; import net.sourceforge.fenixedu.domain.contents.Content; import net.sourceforge.fenixedu.domain.contents.Element; import net.sourceforge.fenixedu.domain.contents.ExplicitOrderNode; import net.sourceforge.fenixedu.domain.contents.FunctionalityCall; import net.sourceforge.fenixedu.domain.contents.MetaDomainObjectPortal; import net.sourceforge.fenixedu.domain.contents.Node; import net.sourceforge.fenixedu.domain.messaging.Forum; import net.sourceforge.fenixedu.injectionCode.IGroup; import pt.utl.ist.fenix.tools.util.i18n.MultiLanguageString; /** * @author Ivo Brandão */ public abstract class Site extends Site_Base { public Site() { super(); setRootDomainObject(RootDomainObject.getInstance()); } @Override public boolean isChildAccepted(Content child) { return child instanceof Section || child instanceof FunctionalityCall || Forum.class.isAssignableFrom(child.getClass()); } public Section createSection(MultiLanguageString sectionName, Container parentContainer, Integer sectionOrder) { return new Section(parentContainer, sectionName, sectionOrder); } public abstract IGroup getOwner(); public List
getTopLevelSections() { return getAssociatedSections(null); } public List
getAllAssociatedSections() { List
sections = new ArrayList
(); for (Section section : getTopLevelSections()) { sections.add(section); sections.addAll(section.getSubSections()); } return sections; } public List
getAssociatedSections(final Section parentSection) { final List
result; if (parentSection != null) { result = parentSection.getAssociatedSections(); } else { result = new ArrayList
(); result.addAll(getChildren(Section.class)); } return result; } public SortedSet
getOrderedTopLevelSections() { final SortedSet
sections = new TreeSet
(Section.COMPARATOR_BY_ORDER); for (final Section section : getChildren(Section.class)) { sections.add(section); } return sections; } public int getNumberOfTopLevelSections() { return getChildren(Section.class).size(); } public void copySectionsAndItemsFrom(Site siteFrom) { for (Section sectionFrom : siteFrom.getAssociatedSections()) { Section sectionTo = this.createSection(sectionFrom.getName(), this, sectionFrom.getSectionOrder()); sectionTo.copyItemsFrom(sectionFrom); sectionTo.copySubSectionsAndItemsFrom(sectionFrom); } } /** * Obtains a list of all the groups available in the context of this site. * * @return */ public List getContextualPermissionGroups() { List groups = new ArrayList(); groups.add(new EveryoneGroup()); groups.add(new InternalPersonGroup()); return groups; } public void setTopLevelSectionsOrder(List
sections) { // SECTION_ORDER_ADAPTER.updateOrder(this, sections); } public String getAuthorName() { return null; } public ExecutionSemester getExecutionPeriod() { return null; } public MetaDomainObjectPortal getTemplate() { MetaDomainObject metaDomainObject = MetaDomainObject.getMeta(this.getClass()); return metaDomainObject == null ? null : (MetaDomainObjectPortal) metaDomainObject.getAssociatedPortal(); } public boolean isTemplateAvailable() { return getTemplate() != null; } public List getOrderedTemplateSections() { List sections = new ArrayList(); MetaDomainObjectPortal template = getTemplate(); if (template != null) { sections.addAll(template.getChildrenAsContent()); } return sections; } public List getTemplateSections() { return getOrderedTemplateSections(); } public List getAllOrderedTopLevelSections() { List sections = getOrderedTemplateSections(); sections.addAll(getOrderedTopLevelSections()); return sections; } public List getAllTopLevelSections() { List sections = getTemplateSections(); sections.addAll(getTopLevelSections()); return sections; } public static List
getOrderedSections(Collection
sections) { List
orderedSections = new ArrayList
(sections); Collections.sort(orderedSections, Section.COMPARATOR_BY_ORDER); return orderedSections; } /** * If this site has quota policy or not. * * @return true if we should not exceed the size in * {@link #getQuota()} */ public boolean hasQuota() { return false; } /** * The maximum size that can be occupied by files in this site. * * @return the maximum combined sizes (in bytes) of all files in this site. */ public long getQuota() { return 0; } /** * Computes the current size (in bytes) occupied by all the files in this * site. * * @return */ public long getUsedQuota() { long size = 0; for (Section section : getAssociatedSections()) { for (Attachment attachment : section.getChildren(Attachment.class)) { size += attachment.getFile().getSize(); } for (Item item : section.getAssociatedItems()) { for (FileContent file : item.getFileItems()) { size += file.getSize(); } } } return size; } public boolean isFileClassificationSupported() { return false; } public boolean isScormContentAccepted() { return false; } public boolean hasAnyAssociatedSections() { return !getAssociatedSections().isEmpty(); } public List
getAssociatedSections() { return (List
) getChildren(Section.class); } public Set
getAssociatedSectionsSet() { Set
sections = new HashSet
(); sections.addAll(getAssociatedSections()); return sections; } public int getAssociatedSectionsCount() { return getAssociatedSections().size(); } public void addAssociatedSections(Section section) { new ExplicitOrderNode(this, section); } @Override public Collection getOrderedChildrenNodes(Class childType) { List nodes = getChildren(); for (Iterator nodeIterator = nodes.iterator(); nodeIterator.hasNext();) { Node node = nodeIterator.next(); if (!childType.isAssignableFrom(node.getClass())) { nodeIterator.remove(); } } return nodes; } @Override public Collection getOrderedChildren(Class type) { List contents = new ArrayList(); for (Node node : new TreeSet(super.getChildren())) { T parent = (T) node.getChild(); if (type != null && !type.isAssignableFrom(parent.getClass())) { continue; } contents.add(parent); } return contents; } @Override public Collection getOrderedChildrenNodes() { List nodes = new ArrayList(); MetaDomainObject template = MetaDomainObject.getMeta(this.getClass()); if (template != null && template.isPortalAvailable()) { nodes.addAll(template.getAssociatedPortal().getOrderedChildrenNodes()); } nodes.addAll(new TreeSet(super.getChildren())); return nodes; } @Override public List getChildren() { List nodes = new ArrayList(); MetaDomainObject template = MetaDomainObject.getMeta(this.getClass()); if (template != null && template.isPortalAvailable()) { nodes.addAll(template.getAssociatedPortal().getChildren()); } nodes.addAll(super.getChildren()); return nodes; } @Override public Collection getDirectChildrenAsContent() { List contents = new ArrayList(); for (Node node : getOrderedDirectChildren()) { contents.add(node.getChild()); } return contents; } @Override public Collection getOrderedDirectChildren() { return new TreeSet(super.getChildren()); } @Override public MultiLanguageString getName() { return MultiLanguageString.i18n().add("pt", String.valueOf(getIdInternal())).finish(); } @Override protected Container findSomeNonModuleParent() { return getTemplate(); } public Collection getAssociatedFunctionalities() { List functionalities = new ArrayList(); for (Content content : getDirectChildrenAsContent()) { if (content instanceof FunctionalityCall) { functionalities.add((FunctionalityCall) content); } } return functionalities; } @Override public Content getInitialContent() { final MetaDomainObjectPortal template = getTemplate(); Content initialContent = null; if (template != null) { initialContent = getTemplate().getInitialContent(); if (initialContent != null) { return initialContent; } Collection children = getOrderedChildren(Element.class); if (!children.isEmpty()) { return children.iterator().next(); } for (Container container : getOrderedChildren(Container.class)) { initialContent = container.getInitialContent(); if (initialContent != null) { break; } } } return initialContent; } @Override protected void disconnect() { for (Node node : getChildrenSet()) { removeNode(node); } disconnectContent(); } @Override protected boolean checkDisconnected() { if (hasRootDomainObject()) return false; if (hasAnyInitialContainer()) return false; if (hasAvailabilityPolicy()) return false; if (hasPortal()) return false; if (hasAnyParents()) return false; if (hasCreator()) return false; if (hasAnyChildren()) return false; if (hasPortalRootDomainObject()) return false; return true; } }