001    /*
002     * 
003     * $Revision: 13574 $ $Date: 2008-06-05 09:40:51 +0200 (Do, 05 Jun 2008) $
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    
024    package org.mycore.datamodel.common;
025    
026    import java.util.Collection;
027    import java.util.HashSet;
028    
029    import org.mycore.common.events.MCREvent;
030    import org.mycore.common.events.MCREventHandlerBase;
031    import org.mycore.datamodel.classifications2.MCRCategLinkServiceFactory;
032    import org.mycore.datamodel.classifications2.MCRCategoryDAOFactory;
033    import org.mycore.datamodel.classifications2.MCRCategoryID;
034    import org.mycore.datamodel.classifications2.MCRObjectReference;
035    import org.mycore.datamodel.metadata.MCRMetaClassification;
036    import org.mycore.datamodel.metadata.MCRMetaElement;
037    import org.mycore.datamodel.metadata.MCRMetaInterface;
038    import org.mycore.datamodel.metadata.MCRMetaLink;
039    import org.mycore.datamodel.metadata.MCRMetaLinkID;
040    import org.mycore.datamodel.metadata.MCRObject;
041    import org.mycore.datamodel.metadata.MCRObjectID;
042    import org.mycore.datamodel.metadata.MCRObjectMetadata;
043    import org.mycore.datamodel.metadata.MCRObjectStructure;
044    
045    /**
046     * This class manages all operations of the LinkTables for operations of an
047     * object.
048     * 
049     * @author Jens Kupferschmidt
050     */
051    public class MCRLinkTableEventHandler extends MCREventHandlerBase {
052    
053        static MCRLinkTableManager mcr_linktable = MCRLinkTableManager.instance();
054    
055        /**
056         * This method add the data to the link and classification table via
057         * MCRLinkTableManager.
058         * 
059         * @param evt
060         *            the event that occured
061         * @param obj
062         *            the MCRObject that caused the event
063         */
064        protected final void handleObjectCreated(MCREvent evt, MCRObject obj) {
065            MCRObjectID mcr_id = obj.getId();
066            // set new entries
067            MCRObjectMetadata meta = obj.getMetadata();
068            MCRMetaElement elm = null;
069            MCRMetaInterface inf = null;
070            if (false) {
071                // TODO: add undo events
072                checkLinkTargets(obj);
073            }
074            //use Set for category collection to remove duplicates if there are any
075            Collection<MCRCategoryID> categories = new HashSet<MCRCategoryID>();
076            for (int i = 0; i < meta.size(); i++) {
077                elm = meta.getMetadataElement(i);
078                for (int j = 0; j < elm.size(); j++) {
079                    inf = elm.getElement(j);
080                    if (inf instanceof MCRMetaClassification) {
081                        String classId = ((MCRMetaClassification) inf).getClassId();
082                        String categId = ((MCRMetaClassification) inf).getCategId();
083                        categories.add(new MCRCategoryID(classId, categId));
084                        continue;
085                    }
086                    if (inf instanceof MCRMetaLinkID) {
087                        mcr_linktable.addReferenceLink(mcr_id.toString(), ((MCRMetaLink) inf).getXLinkHref(), MCRLinkTableManager.ENTRY_TYPE_REFERENCE, "");
088                        continue;
089                    }
090                }
091            }
092            if (categories.size() > 0) {
093                MCRObjectReference objectReference = new MCRObjectReference(mcr_id.toString(), mcr_id.getTypeId());
094                MCRCategLinkServiceFactory.getInstance().setLinks(objectReference, categories);
095            }
096            // add derivate referece
097            MCRObjectStructure struct = obj.getStructure();
098            int dersize = struct.getDerivateSize();
099            for (int i = 0; i < dersize; i++) {
100                MCRMetaLinkID lid = struct.getDerivate(i);
101                mcr_linktable.addReferenceLink(obj.getId(), lid.getXLinkHrefID(), MCRLinkTableManager.ENTRY_TYPE_DERIVATE, "");
102            }
103    
104        }
105    
106        private void deleteOldLinks(MCRObjectID mcr_id) {
107            mcr_linktable.deleteReferenceLink(mcr_id);
108            MCRCategLinkServiceFactory.getInstance().deleteLink(mcr_id.getId());
109        }
110    
111        private void checkLinkTargets(MCRObject obj) {
112            MCRObjectID mcr_id = obj.getId();
113            MCRObjectMetadata meta = obj.getMetadata();
114            MCRMetaElement elm;
115            MCRMetaInterface inf;
116            for (int i = 0; i < meta.size(); i++) {
117                elm = meta.getMetadataElement(i);
118                for (int j = 0; j < elm.size(); j++) {
119                    inf = elm.getElement(j);
120                    if (inf instanceof MCRMetaClassification) {
121                        String classID = ((MCRMetaClassification) inf).getClassId();
122                        String categID = ((MCRMetaClassification) inf).getCategId();
123                        boolean exists = MCRCategoryDAOFactory.getInstance().exist(new MCRCategoryID(classID, categID));
124                        if (exists) {
125                            continue;
126                        }
127                        MCRActiveLinkException activeLink = new MCRActiveLinkException("Failure while adding link!. Destination does not exist.");
128                        String destination = classID + "##" + categID;
129                        activeLink.addLink(mcr_id.toString(), destination);
130                        // throw activeLink;
131                        // TODO: should trigger undo-Event
132                    }
133                    if (inf instanceof MCRMetaLinkID) {
134                        String destination = ((MCRMetaLinkID) inf).getXLinkHref();
135                        if (!MCRXMLTableManager.instance().exist(new MCRObjectID(destination))) {
136                            continue;
137                        }
138                        MCRActiveLinkException activeLink = new MCRActiveLinkException("Failure while adding link!. Destination does not exist.");
139                        activeLink.addLink(mcr_id.toString(), destination);
140                        // throw activeLink;
141                        // TODO: should trigger undo-Event
142                    }
143                }
144            }
145        }
146    
147        /**
148         * This method update the data to the link and classification table via
149         * MCRLinkTableManager.
150         * 
151         * @param evt
152         *            the event that occured
153         * @param obj
154         *            the MCRObject that caused the event
155         */
156        protected final void handleObjectUpdated(MCREvent evt, MCRObject obj) {
157            handleObjectRepaired(evt, obj);
158        }
159    
160        /**
161         * This method delete the data from the link and classification table via
162         * MCRLinkTableManager.
163         * 
164         * @param evt
165         *            the event that occured
166         * @param obj
167         *            the MCRObject that caused the event
168         */
169        protected final void handleObjectDeleted(MCREvent evt, MCRObject obj) {
170            deleteOldLinks(obj.getId());
171        }
172    
173        /**
174         * This method repair the data from the link and classification table via
175         * MCRLinkTableManager.
176         * 
177         * @param evt
178         *            the event that occured
179         * @param obj
180         *            the MCRObject that caused the event
181         */
182        protected final void handleObjectRepaired(MCREvent evt, MCRObject obj) {
183            handleObjectDeleted(evt, obj);
184            handleObjectCreated(evt, obj);
185        }
186    
187    }