001/*
002 *  Copyright 2014 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.plugins.pagesubscription.observer;
017
018import java.util.ArrayList;
019import java.util.Collection;
020import java.util.Collections;
021import java.util.Iterator;
022import java.util.List;
023import java.util.Map;
024
025import org.ametys.core.observation.Event;
026import org.ametys.core.util.URIUtils;
027import org.ametys.plugins.explorer.ObservationConstants;
028import org.ametys.plugins.explorer.resources.Resource;
029import org.ametys.plugins.repository.AmetysObject;
030import org.ametys.runtime.i18n.I18nizableText;
031import org.ametys.web.repository.page.Page;
032
033/**
034 * Listen attachment page and send mail
035 */
036public class PageSubscriptionExplorerObserver extends AbstractPageSubscriptionObserver
037{
038    private static final int __MAX_RESOURCE_LINK = 20;
039
040    @Override
041    public boolean supports(Event event)
042    {
043        String eventType = event.getId();
044        return eventType.equals(ObservationConstants.EVENT_RESOURCE_CREATED) || eventType.equals(ObservationConstants.EVENT_RESOURCE_UPDATED)
045                || eventType.equals(ObservationConstants.EVENT_RESOURCE_DELETED);
046    }
047    
048    @Override
049    protected String getSiteParameterId()
050    {
051        return "page-subscription-attachments";
052    }
053
054    private String _getAbsoluteResourceUrl(Map<String, Resource> resources, Page page)
055    {
056        if (resources.size() == 1)
057        {
058            return _getAbsoluteResourceUrl(resources.values().iterator().next(), page);
059        }
060        else
061        {
062            StringBuilder sb = new StringBuilder();
063
064            int nbResources = resources.size();
065            Iterator<String> it = resources.keySet().iterator();
066
067            int count = 0;
068            while (it.hasNext() && count < __MAX_RESOURCE_LINK)
069            {
070                String id = it.next();
071                Resource resource = resources.get(id);
072                sb.append("\n").append("* ").append(resource.getName()).append(" : ").append(_getAbsoluteResourceUrl(resource, page));
073
074                count++;
075            }
076
077            if (nbResources - __MAX_RESOURCE_LINK > 0)
078            {
079                List<String> i18nparams = new ArrayList<>();
080                i18nparams.add(String.valueOf(nbResources - __MAX_RESOURCE_LINK));
081                I18nizableText moreLinks = new I18nizableText("plugin.page-subscription", "PLUGINS_PAGE_SUBSCRIBE_SEND_MAIL_CREATE_MULTIPLE_MORE_LINKS", i18nparams);
082                sb.append(_i18nUtils.translate(moreLinks));
083            }
084
085            return sb.toString();
086        }
087    }
088
089    private String _getAbsoluteResourceUrl(Resource resource, Page page)
090    {
091        return new StringBuilder().append(page.getSite().getUrl()).append("/_page-attachment").append("?id=").append(URIUtils.encodeParameter(resource.getId())).append(
092                "&download=true").toString();
093    }
094
095    @Override
096    protected String _getMailSubject(Event event, Page page)
097    {
098        Map<String, Object> args = event.getArguments();
099
100        List<String> i18nparam = new ArrayList<>();
101        i18nparam.add(page.getTitle()); // {0}
102
103        String i18nKey = null;
104        switch (event.getId())
105        {
106            case ObservationConstants.EVENT_RESOURCE_CREATED:
107                @SuppressWarnings("unchecked")
108                Map<String, Resource> resources = (Map<String, Resource>) args.get(ObservationConstants.ARGS_RESOURCES);
109                if (resources.size() > 1)
110                {
111                    i18nKey = "PLUGINS_PAGE_SUBSCRIBE_SEND_MAIL_CREATE_MULTIPLE_TITLE";
112                    i18nparam.add(String.valueOf(resources.size())); // {1}
113                }
114                else
115                {
116                    i18nKey = "PLUGINS_PAGE_SUBSCRIBE_SEND_MAIL_CREATE_TITLE";
117                }
118                break;
119            case ObservationConstants.EVENT_RESOURCE_UPDATED:
120                i18nKey = "PLUGINS_PAGE_SUBSCRIBE_SEND_MAIL_UPDATE_TITLE";
121                break;
122            case ObservationConstants.EVENT_RESOURCE_DELETED:
123                i18nKey = "PLUGINS_PAGE_SUBSCRIBE_SEND_MAIL_DELETE_TITLE";
124                break;
125            default:
126                break;
127        }
128
129        I18nizableText i18nSubject = new I18nizableText("plugin.page-subscription", i18nKey, i18nparam);
130        String subject = _i18nUtils.translate(i18nSubject);
131        return subject;
132    }
133
134    @Override
135    protected String _getMailBody(Event event, Page page)
136    {
137        Map<String, Object> args = event.getArguments();
138        List<String> i18nparam = new ArrayList<>();
139
140        i18nparam.add(_getAbsolutePageUrl(page)); // {0}
141        i18nparam.add(page.getTitle()); // {1}
142        i18nparam.add(page.getSite().getTitle()); // {2}
143
144        I18nizableText i18nTextBody = null;
145        switch (event.getId())
146        {
147            case ObservationConstants.EVENT_RESOURCE_CREATED:
148                @SuppressWarnings("unchecked")
149                Map<String, Resource> resources = (Map<String, Resource>) args.get(ObservationConstants.ARGS_RESOURCES);
150
151                String i18nKey = null;
152                if (resources.size() > 1)
153                {
154                    i18nKey = "PLUGINS_PAGE_SUBSCRIBE_SEND_MAIL_CREATE_MULTIPLE_TEXT_BODY";
155                    i18nparam.add(String.valueOf(resources.size())); // {3}
156                }
157                else
158                {
159                    i18nKey = "PLUGINS_PAGE_SUBSCRIBE_SEND_MAIL_CREATE_TEXT_BODY";
160                    Resource resource = _resolver.resolveById(resources.keySet().iterator().next());
161                    i18nparam.add(resource.getName()); // {3}
162                }
163
164                i18nparam.add(_getAbsoluteResourceUrl(resources, page)); // {4}
165
166                i18nTextBody = new I18nizableText("plugin.page-subscription", i18nKey, i18nparam);
167                break;
168
169            case ObservationConstants.EVENT_RESOURCE_UPDATED:
170                String resourceId = (String) event.getArguments().get(ObservationConstants.ARGS_ID);
171                Resource resource = _resolver.resolveById(resourceId);
172
173                i18nparam.add(resource.getName());
174                i18nparam.add(_getAbsoluteResourceUrl(resource, page));
175
176                i18nTextBody = new I18nizableText("plugin.page-subscription", "PLUGINS_PAGE_SUBSCRIBE_SEND_MAIL_UPDATE_TEXT_BODY", i18nparam);
177                break;
178
179            case ObservationConstants.EVENT_RESOURCE_DELETED:
180                i18nparam.add((String) event.getArguments().get(ObservationConstants.ARGS_NAME));
181
182                i18nTextBody = new I18nizableText("plugin.page-subscription", "PLUGINS_PAGE_SUBSCRIBE_SEND_MAIL_DELETE_TEXT_BODY", i18nparam);
183                break;
184            default:
185                break;
186        }
187
188        String textBody = _i18nUtils.translate(i18nTextBody);
189        return textBody;
190    }
191
192    @Override
193    protected Collection<Page> _getPages(Event event)
194    {
195        AmetysObject object = null;
196        Map<String, Object> args = event.getArguments();
197        if (event.getId().equals(ObservationConstants.EVENT_RESOURCE_DELETED))
198        {
199            String parentId = (String) args.get(ObservationConstants.ARGS_PARENT_ID);
200            object = _resolver.resolveById(parentId);
201        }
202        else if (event.getId().equals(ObservationConstants.EVENT_RESOURCE_CREATED))
203        {
204            String parentId = (String) args.get(ObservationConstants.ARGS_PARENT_ID);
205            object = _resolver.resolveById(parentId);
206        }
207        else
208        {
209            String targetId = (String) args.get(ObservationConstants.ARGS_ID);
210            object = _resolver.resolveById(targetId);
211        }
212        
213        AmetysObject parent = object.getParent();
214        while (parent != null)
215        {
216            if (parent instanceof Page)
217            {
218                return Collections.singletonList((Page) parent);
219            }
220            parent = parent.getParent();
221        }
222        
223        return Collections.EMPTY_LIST;
224    }
225
226}