<commit_before>/*
 * Copyright  1999-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.apache.lenya.cms.repository;

import java.io.File;
import java.io.IOException;

import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.logger.Logger;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.excalibur.source.Source;
import org.apache.excalibur.source.SourceResolver;
import org.apache.lenya.ac.Identity;
import org.apache.lenya.ac.User;
import org.apache.lenya.cms.cocoon.source.SourceUtil;
import org.apache.lenya.cms.publication.Publication;
import org.apache.lenya.cms.publication.PublicationFactory;
import org.apache.lenya.cms.rc.RCEnvironment;
import org.apache.lenya.cms.rc.RevisionController;
import org.apache.lenya.transaction.IdentityMap;
import org.apache.lenya.transaction.Lock;
import org.apache.lenya.transaction.TransactionException;
import org.w3c.dom.Document;

/**
 * A repository node.
 * 
 * @version $Id:$
 */
public class SourceNode extends AbstractLogEnabled implements Node {

    private Document document;
    private String sourceUri;
    private ServiceManager manager;
    private IdentityMap identityMap;

    /**
     * Ctor.
     * @param map
     * @param sourceUri
     * @param manager
     * @param logger
     */
    public SourceNode(IdentityMap map, String sourceUri, ServiceManager manager, Logger logger) {
        this.sourceUri = sourceUri;
        this.manager = manager;
        enableLogging(logger);
        this.identityMap = map;
    }

    /**
     * @see org.apache.lenya.cms.repository.Node#getDocument()
     */
    public Document getDocument() {
        try {
            if (this.document == null && SourceUtil.exists(getRealSourceURI(), this.manager)) {
                this.document = SourceUtil.readDOM(getRealSourceURI(), this.manager);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return this.document;
    }

    protected String getUserId() {
        String userId = null;
        Identity identity = this.identityMap.getUnitOfWork().getIdentity();
        if (identity != null) {
            User user = identity.getUser();
            if (user != null) {
                userId = user.getId();
            }
        }
        return userId;
    }

    protected String getRealSourceURI() {
        return "context://" + this.sourceUri.substring("lenya://".length());
    }

    /**
     * @see org.apache.lenya.transaction.Transactionable#checkin()
     */
    public void checkin() throws TransactionException {
        if (!isCheckedOut()) {
            throw new TransactionException("Cannot check in node [" + this.sourceUri
                    + "]: not checked out!");
        }

        try {
            String userName = getUserId();
            boolean newVersion = this.identityMap.getUnitOfWork().isDirty(this);
            getRevisionController().reservedCheckIn(getRCPath(), userName, true, newVersion);
            this.isCheckedOut = false;
        } catch (Exception e) {
            throw new TransactionException(e);
        }
    }

    private boolean isCheckedOut = false;

    /**
     * @see org.apache.lenya.transaction.Transactionable#isCheckedOut()
     */
    public boolean isCheckedOut() throws TransactionException {
        return isCheckedOut;
    }

    /**
     * @see org.apache.lenya.transaction.Transactionable#checkout()
     */
    public void checkout() throws TransactionException {
        if (!isCheckedOut()) {
            try {
                getRevisionController().reservedCheckOut(getRCPath(), getUserId());
                this.isCheckedOut = true;
            } catch (Exception e) {
                throw new TransactionException(e);
            }
        }<commit_msg>using revision controller to determine if source node is checked out<commit_after>/*
 * Copyright  1999-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.apache.lenya.cms.repository;

import java.io.File;
import java.io.IOException;

import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.logger.Logger;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.excalibur.source.Source;
import org.apache.excalibur.source.SourceResolver;
import org.apache.lenya.ac.Identity;
import org.apache.lenya.ac.User;
import org.apache.lenya.cms.cocoon.source.SourceUtil;
import org.apache.lenya.cms.publication.Publication;
import org.apache.lenya.cms.publication.PublicationFactory;
import org.apache.lenya.cms.rc.RCEnvironment;
import org.apache.lenya.cms.rc.RevisionController;
import org.apache.lenya.transaction.IdentityMap;
import org.apache.lenya.transaction.Lock;
import org.apache.lenya.transaction.TransactionException;
import org.w3c.dom.Document;

/**
 * A repository node.
 * 
 * @version $Id:$
 */
public class SourceNode extends AbstractLogEnabled implements Node {

    private Document document;
    private String sourceUri;
    private ServiceManager manager;
    private IdentityMap identityMap;

    /**
     * Ctor.
     * @param map
     * @param sourceUri
     * @param manager
     * @param logger
     */
    public SourceNode(IdentityMap map, String sourceUri, ServiceManager manager, Logger logger) {
        this.sourceUri = sourceUri;
        this.manager = manager;
        enableLogging(logger);
        this.identityMap = map;
    }

    /**
     * @see org.apache.lenya.cms.repository.Node#getDocument()
     */
    public Document getDocument() {
        try {
            if (this.document == null && SourceUtil.exists(getRealSourceURI(), this.manager)) {
                this.document = SourceUtil.readDOM(getRealSourceURI(), this.manager);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return this.document;
    }

    protected String getUserId() {
        String userId = null;
        Identity identity = this.identityMap.getUnitOfWork().getIdentity();
        if (identity != null) {
            User user = identity.getUser();
            if (user != null) {
                userId = user.getId();
            }
        }
        return userId;
    }

    protected String getRealSourceURI() {
        return "context://" + this.sourceUri.substring("lenya://".length());
    }

    /**
     * @see org.apache.lenya.transaction.Transactionable#checkin()
     */
    public void checkin() throws TransactionException {
        if (!isCheckedOut()) {
            throw new TransactionException("Cannot check in node [" + this.sourceUri
                    + "]: not checked out!");
        }

        try {
            String userName = getUserId();
            boolean newVersion = this.identityMap.getUnitOfWork().isDirty(this);
            getRevisionController().reservedCheckIn(getRCPath(), userName, true, newVersion);
        } catch (Exception e) {
            throw new TransactionException(e);
        }
    }

    /**
     * @see org.apache.lenya.transaction.Transactionable#isCheckedOut()
     */
    public boolean isCheckedOut() throws TransactionException {
        try {
            return getRevisionController().isCheckedOut(getRCPath());
        } catch (TransactionException e) {
            throw e;
        } catch (Exception e) {
            throw new TransactionException(e);
        }
    }

    /**
     * @see org.apache.lenya.transaction.Transactionable#checkout()
     */
    public void checkout() throws TransactionException {
        if (!isCheckedOut()) {
            try {
                getRevisionController().reservedCheckOut(getRCPath(), getUserId());
            } catch (Exception e) {
                throw new TransactionException(e);
            }
        }
{'fraction_non_alphanumeric': 0.10400538539212387, 'fraction_numerical': 0.0029170874004263436, 'mean_word_length': 2.6176948051948052, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5392728', 'n_tokens_mistral': 2418, 'n_tokens_neox': 2364, 'n_words': 600}