001/*
002 *  Copyright 2011 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.web.repository.content;
017
018import org.slf4j.Logger;
019import org.slf4j.LoggerFactory;
020
021import org.ametys.plugins.repository.AmetysObject;
022import org.ametys.plugins.repository.AmetysRepositoryException;
023import org.ametys.plugins.repository.UnknownAmetysObjectException;
024
025/**
026 * This handler look for links, images or video url which make reference to a AmetysObject (content, page, resources, ...) in a RichText.
027 * It must be used after a copy of a Content to changes theses references if necessary.
028 */
029public class WebDocbookUpdateHandler extends org.ametys.cms.contenttype.DefaultDocbookUpdateHandler
030{
031    private static Logger _logger = LoggerFactory.getLogger(WebDocbookUpdateHandler.class.getName());
032    
033    @Override
034    protected String _getUpdatedAmetysObjectId (String id)
035    {
036        try
037        {
038            if (_resolver.hasAmetysObjectForId(id))
039            {
040                AmetysObject ametysObject = _resolver.resolveById(id);
041                String path = ametysObject.getPath();
042                
043                if (path.startsWith(_initialAOPath))
044                {
045                    // Is in path
046                    String relPath = path.equals(_initialAOPath) ? "" : path.substring(_initialAOPath.length() + 1);
047                    try
048                    {
049                        // Find symmetric object on copied sub-tree
050                        AmetysObject child = "".equals(relPath) ? _createdObject : _createdObject.getChild(relPath);
051                        return child.getId();
052                    }
053                    catch (UnknownAmetysObjectException e)
054                    {
055                        _logger.warn("Object of path " + relPath + " was not found on copied sub-tree " + _createdObject.getPath(), e);
056                    }
057                }
058                else if (path.equals(_initialContentPath))
059                {
060                    // Return id of the new content
061                    return _createdContent.getId();
062                }
063                else if (path.startsWith(_initialContentPath))
064                {
065                    // Is in path of the content
066                    String relPath = path.substring(_initialContentPath.length() + 1);
067                    
068                    if (_createdContent instanceof WebContent)
069                    {
070                        // Find symmetric object on copied sub-tree
071                        AmetysObject child = ((WebContent) _createdContent).getChild(relPath);
072                        return child.getId();
073                    }
074                }
075            }
076            
077            // Return same id
078            return id;
079        }
080        catch (AmetysRepositoryException e)
081        {
082            // Return same id
083            return id;
084        }
085    }
086}