001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.site.usecases;
019:
020: import java.util.Iterator;
021: import java.util.Map;
022: import java.util.Vector;
023: import java.util.WeakHashMap;
024:
025: import javax.servlet.http.HttpSession;
026:
027: import org.apache.lenya.ac.Identity;
028: import org.apache.lenya.ac.User;
029: import org.apache.lenya.cms.metadata.dublincore.DublinCoreHelper;
030: import org.apache.lenya.cms.publication.Document;
031: import org.apache.lenya.cms.repository.Node;
032: import org.apache.lenya.cms.usecase.DocumentUsecase;
033: import org.apache.lenya.util.SessionListener;
034:
035: /**
036: * Force a document to be checked in (override a checkout).
037: */
038: public class ForceCheckIn extends DocumentUsecase {
039:
040: private static final String IDENTITY = Identity.class.getName();
041: private static final String HISTORY = "org.apache.lenya.cms.cocoon.acting.History";
042:
043: protected void doCheckPreconditions() throws Exception {
044: super .doCheckPreconditions();
045:
046: Document doc = getSourceDocument();
047: if (doc == null) {
048: return;
049: }
050:
051: Node node = doc.getRepositoryNode();
052: if (!node.isCheckedOut()) {
053: String[] params = { DublinCoreHelper.getTitle(doc, true) };
054: addErrorMessage("not-checked-out", params);
055: }
056: }
057:
058: protected void prepareView() throws Exception {
059: Node node = getSourceDocument().getRepositoryNode();
060: if (node.isCheckedOut()) {
061: String userId = node.getCheckoutUserId();
062: String[] params = { userId };
063: if (isLoggedIn(userId)) {
064: addInfoMessage("user-logged-in", params);
065: } else {
066: addInfoMessage("user-not-logged-in", params);
067: }
068: }
069: }
070:
071: protected boolean isLoggedIn(String userId) {
072: String pubId = getSourceDocument().getPublication().getId();
073: SessionListener sessions = new SessionListener();
074: WeakHashMap allSessions = sessions.getAllSessions();
075:
076: boolean loggedIn = false;
077: Iterator userit = allSessions.entrySet().iterator();
078: while (userit.hasNext()) {
079: Map.Entry entry = (Map.Entry) userit.next();
080: HttpSession nextsession = (HttpSession) entry.getValue();
081:
082: Identity identity = (Identity) nextsession
083: .getAttribute(IDENTITY);
084:
085: if (identity == null) {
086: continue;
087: }
088:
089: User user = identity.getUser();
090: Vector history = (Vector) nextsession.getAttribute(HISTORY);
091: String publicationID = getPublicationIDfromHistory(history);
092: if (publicationID.equals(pubId) && user != null
093: && user.getId().equals(userId)) {
094: loggedIn = true;
095: }
096: }
097: return loggedIn;
098: }
099:
100: protected void doExecute() throws Exception {
101: super .doExecute();
102:
103: Document doc = getSourceDocument();
104: Node node = doc.getRepositoryNode();
105:
106: node.forceCheckIn();
107: }
108:
109: /**
110: * Extract the publicationID from the history information stored in the
111: * session
112: *
113: * @param history The history stored in the session.. An entry looks like \/
114: * <publication-id>/authoring/index.html
115: * @return A publication ID.
116: */
117: private String getPublicationIDfromHistory(Vector history) {
118: String firstElement = history.firstElement().toString();
119: String publicationID = firstElement.substring(1, firstElement
120: .indexOf("/", 1));
121: return publicationID;
122: }
123:
124: }
|