1 package org.exoplatform.services.wcm.extensions.publication.impl;
2
3 import java.time.OffsetDateTime;
4 import java.time.format.DateTimeFormatter;
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 import javax.jcr.Node;
11
12 import org.exoplatform.container.component.ComponentPlugin;
13 import org.exoplatform.services.log.ExoLogger;
14 import org.exoplatform.services.log.Log;
15 import org.exoplatform.services.security.Identity;
16 import org.exoplatform.services.security.IdentityRegistry;
17 import org.exoplatform.services.wcm.extensions.publication.PublicationManager;
18 import org.exoplatform.services.wcm.extensions.publication.context.ContextPlugin;
19 import org.exoplatform.services.wcm.extensions.publication.context.impl.ContextConfig.Context;
20 import org.exoplatform.services.wcm.extensions.publication.lifecycle.StatesLifecyclePlugin;
21 import org.exoplatform.services.wcm.extensions.publication.lifecycle.impl.LifecyclesConfig.Lifecycle;
22 import org.exoplatform.services.wcm.extensions.publication.lifecycle.impl.LifecyclesConfig.State;
23 import org.exoplatform.services.wcm.publication.WCMComposer;
24 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
25 import org.picocontainer.Startable;
26
27
28
29
30 public class PublicationManagerImpl implements PublicationManager, Startable {
31
32 private Map<String, Lifecycle> lifecycles = new HashMap<String, Lifecycle>();
33
34 private Map<String, Context> contexts = new HashMap<String, Context>();
35
36 private static final Log LOG = ExoLogger.getLogger(PublicationManagerImpl.class.getName());
37
38 public void addLifecycle(ComponentPlugin plugin) {
39 if (plugin instanceof StatesLifecyclePlugin) {
40 if (((StatesLifecyclePlugin) plugin).getLifecyclesConfig() != null) {
41 for (Lifecycle l:((StatesLifecyclePlugin) plugin).getLifecyclesConfig().getLifecycles()) {
42 if (LOG.isInfoEnabled()) {
43 LOG.info("Adding Lifecyle : "+l.getName());
44 }
45 lifecycles.put(l.getName(), l);
46 }
47 }
48 }
49 }
50
51 public void removeLifecycle(ComponentPlugin plugin) {
52 if (plugin instanceof StatesLifecyclePlugin) {
53 if (((StatesLifecyclePlugin) plugin).getLifecyclesConfig() != null) {
54 for (Lifecycle l:((StatesLifecyclePlugin) plugin).getLifecyclesConfig().getLifecycles()) {
55 if (lifecycles.get(l.getName())!=null) {
56 if (LOG.isInfoEnabled()) {
57 LOG.info("Removing Lifecyle : "+l.getName());
58 }
59 lifecycles.remove(l.getName());
60 }
61 }
62 }
63 }
64 }
65
66 public void addContext(ComponentPlugin plugin) {
67 if (plugin instanceof ContextPlugin) {
68 if (((ContextPlugin) plugin).getContextConfig() != null) {
69 for (Context c:((ContextPlugin) plugin).getContextConfig().getContexts()) {
70 if (LOG.isInfoEnabled()) {
71 LOG.info("Adding Context : "+c.getName());
72 }
73 contexts.put(c.getName(), c);
74 }
75 }
76 }
77 }
78
79 public void removeContext(ComponentPlugin plugin) {
80 if (plugin instanceof ContextPlugin) {
81 if (((ContextPlugin) plugin).getContextConfig() != null) {
82 for (Context c:((ContextPlugin) plugin).getContextConfig().getContexts()) {
83 if (contexts.get(c.getName())!=null) {
84 if (LOG.isInfoEnabled()) {
85 LOG.info("Removing Context : "+c.getName());
86 }
87 contexts.remove(c.getName());
88 }
89 }
90 }
91 }
92 }
93
94 public void start() {
95
96 }
97
98 public void stop() {
99
100 }
101
102 public Context getContext(String name) {
103 if (contexts.containsKey(name))
104 return contexts.get(name);
105
106 return null;
107 }
108
109 public List<Context> getContexts() {
110 return new ArrayList<Context>(contexts.values());
111 }
112
113 public Lifecycle getLifecycle(String name) {
114 if (lifecycles.containsKey(name))
115 return lifecycles.get(name);
116 return null;
117 }
118
119 public List<Lifecycle> getLifecycles() {
120 return new ArrayList<Lifecycle>(lifecycles.values());
121 }
122
123 public List<Lifecycle> getLifecyclesFromUser(String remoteUser, String state) {
124 List<Lifecycle> lifecycles = null;
125
126 for (Lifecycle lifecycle : getLifecycles()) {
127 if (lifecycles == null)
128 lifecycles = new ArrayList<Lifecycle>();
129 IdentityRegistry identityRegistry = WCMCoreUtils.getService(IdentityRegistry.class);
130 Identity identity = identityRegistry.getIdentity(remoteUser);
131 for (State state_ : lifecycle.getStates()) {
132 if (state.equals(state_.getState())) {
133 List<String> memberships = new ArrayList<String>();
134 if (state_.getMembership() != null && !"automatic".equals(state_.getMembership())) {
135 memberships.add(state_.getMembership());
136 }
137 if (state_.getMemberships() != null)
138 memberships.addAll(state_.getMemberships());
139 for (String membership : memberships) {
140 String[] membershipTab = membership.split(":");
141 if (identity.isMemberOf(membershipTab[1], membershipTab[0])) {
142 lifecycles.add(lifecycle);
143 break;
144 }
145 }
146 }
147 }
148
149 }
150 return lifecycles;
151 }
152
153 public List<Node> getContents(String fromstate,
154 String tostate,
155 String date,
156 String user,
157 String lang,
158 String workspace) throws Exception {
159
160 WCMComposer wcmComposer = WCMCoreUtils.getService(WCMComposer.class);
161
162 HashMap<String, String> filters = new HashMap<String, String>();
163 filters.put(WCMComposer.FILTER_MODE, WCMComposer.MODE_EDIT);
164 filters.put(WCMComposer.FILTER_LANGUAGE, lang);
165 StringBuffer query = new StringBuffer("select * from nt:base where publication:currentState='"
166 + fromstate + "'");
167
168 if (tostate!=null) {
169 List<Lifecycle> lifecycles = this.getLifecyclesFromUser(user, tostate);
170 if (lifecycles!=null && !lifecycles.isEmpty()) {
171 query.append(" and (");
172 boolean first = true;
173 for (Lifecycle lifecycle:lifecycles) {
174 if (!first) query.append(" or ");
175 first = false;
176 query.append("publication:lifecycle='"+lifecycle.getName()+"'");
177 }
178 query.append(")");
179 } else {
180 query.append(" and publication:lifecycle='_no_lifecycle'");
181 }
182 } else if (user!=null) {
183 query.append(" and publication:lastUser='"+user+"'");
184 }
185
186 if (date!=null) {
187 OffsetDateTime startPublishedDateTime = OffsetDateTime.now().plusDays(Integer.parseInt(date));
188 query.append(" and publication:startPublishedDate<=TIMESTAMP '");
189 query.append(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(startPublishedDateTime));
190 query.append("' order by publication:startPublishedDate asc");
191 } else {
192 query.append(" order by exo:dateModified desc");
193 }
194 filters.put(WCMComposer.FILTER_QUERY_FULL, query.toString());
195 if (LOG.isDebugEnabled()) LOG.debug("query="+query.toString());
196 List<Node> nodes = wcmComposer.getContents(workspace, "/", filters, WCMCoreUtils.getUserSessionProvider());
197
198 return nodes;
199 }
200 }