package org.osivia.nuxeo.component; import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuxeo.ecm.core.api.ClientException; import org.nuxeo.ecm.core.api.DocumentModel; import org.nuxeo.runtime.model.ComponentInstance; import org.nuxeo.runtime.model.DefaultComponent; /** * Generic fragment class * */ public class FragmentServiceImpl extends DefaultComponent implements FragmentService { private static final Log log = LogFactory.getLog(FragmentServiceImpl.class); /** Map of all fragment types */ private static final Map fragments = new HashMap(); /** Main nuxeo schema shared by all fragments */ public static String SCHEMA = "fragments"; @Override public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) throws Exception { FragmentDescriptor contribDescriptor = (FragmentDescriptor) contribution; contribDescriptor.initFragment(); addFragmentType(contribDescriptor); } @Override public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) throws Exception { FragmentDescriptor contribDescriptor = (FragmentDescriptor) contribution; removeFragmentType(contribDescriptor); } public void addFragmentType(FragmentDescriptor contribution) { if (fragments.get(contribution) == null) { fragments.put(contribution, contribution.getInstance()); } else { log.warn("Contribution " + contribution.code + " has already been registered."); } } public void removeFragmentType(FragmentDescriptor contribution) { fragments.remove(contribution); } public Map.Entry findByCode(String code) throws FragmentServiceException { for (Map.Entry entry : fragments.entrySet()) { if (entry.getKey().getCode().equals(code)) { return entry; } } throw new FragmentServiceException("osivia.error.fragment_not_found"); } /** * @param doc nuxeo document * @param uri id of the fragment * @return the fragmentCategory */ public Entry getFragmentCategory(DocumentModel doc, String uri) throws FragmentServiceException { String fgtCategory = null; Map properties; try { properties = doc.getProperties(SCHEMA); Collection values = properties.values(); // Une seule liste dans ce schéma Object liste = values.iterator().next(); if (liste instanceof List) { List> listeFragments = (List>) liste; for (Map fragment : listeFragments) { if (uri.equals(fragment.get("uri"))) { fgtCategory = fragment.get("fragmentCategory").toString(); break; } } } } catch (ClientException e) { throw new FragmentServiceException(e); } if (fgtCategory == null) throw new FragmentServiceException("osivia.error.fragment_not_found"); Entry findByCode = findByCode(fgtCategory); return findByCode; } /** * Initialize a default entry in the main fragments schema * * @param doc the current simplepage * @param region the cms region * @param belowUri the position where the fragment goes * @param code2 * @return the new id (timestamp) */ public String prepareCreation(DocumentModel doc, Fragment specific, String fragmentCategory, String region, String belowUri, String code2) throws FragmentServiceException { String uri = null; Map properties; try { properties = doc.getProperties(SCHEMA); Collection values = properties.values(); // Une seule liste dans ce schéma Object liste = values.iterator().next(); if (liste instanceof List) { List> listeFragments = (List>) liste; // ================== Calcul des propriétés du schéma // Génération d'une URI uri = Long.toString(new Date().getTime()); // Calcul du positionnement // Par défaut en haut de la région (0) String regionId = region; Integer order = new Integer(0); // Si l'uri du fragment au dessus est précisé, récupération de // la position et de la région du fragment if (belowUri != null) { for (Map fragment : listeFragments) { if (belowUri.equals(fragment.get("uri"))) { regionId = fragment.get("regionId").toString(); String orderStr = fragment.get("order").toString(); order = Integer.parseInt(orderStr) + 1; break; } } } // ================== Décalage des autres fragments de cette région for (Map fragment : listeFragments) { String regionCompare = fragment.get("regionId").toString(); String orderCompare = fragment.get("order").toString(); if (regionId.equals(regionCompare) && Integer.parseInt(orderCompare) >= order) { Integer newOrder = Integer.parseInt(orderCompare) + 1; fragment.put("order", newOrder.toString()); } } // ================== Ajout d'une nouvelle entrée au schéma Map newEntry = new HashMap(); newEntry.put("fragmentCategory", fragmentCategory); newEntry.put("order", order.toString()); newEntry.put("regionId", regionId); newEntry.put("uri", uri); newEntry.put("title", "Nouveau fragment"); newEntry.put("hideTitle", Boolean.FALSE.toString()); newEntry.put("style", ""); listeFragments.add(newEntry); doc.setProperties(SCHEMA, properties); } } catch (ClientException e) { throw new FragmentServiceException(e); } specific.prepareCreation(doc, uri, region, belowUri, code2); return uri; } }