<commit_before>/*
 * (C) Copyright 2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     Nuxeo - initial API and implementation
 *
 * $Id: CreateDocumentRestlet.java 30586 2008-02-26 14:30:17Z ogrisel $
 */

package org.nuxeo.ecm.platform.ui.web.restAPI;

import static org.jboss.seam.ScopeType.EVENT;

import org.dom4j.Element;
import org.dom4j.dom.DOMDocument;
import org.dom4j.dom.DOMDocumentFactory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.nuxeo.common.utils.IdUtils;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.IdRef;
import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
import org.nuxeo.ecm.platform.ui.web.tag.fn.LiveEditConstants;
import org.nuxeo.ecm.platform.util.RepositoryLocation;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;

/**
 * Allow the creation of a new document of the specified document type
 *
 * @author <NAME> <<EMAIL>>
 */
@Name("createDocumentRestlet")
@Scope(EVENT)
public class CreateDocumentRestlet extends BaseNuxeoRestlet implements
        LiveEditConstants {

    @In(create = true)
    protected NavigationContext navigationContext;

    protected CoreSession documentManager;

    @Override
    public void handle(Request req, Response res) {
        String repo = (String) req.getAttributes().get("repo");
        if (repo == null || repo.equals("*")) {
            handleError(res, "you must specify a repository");
            return;
        }

        DocumentModel parentDm = null;
        try {
            navigationContext.setCurrentServerLocation(new RepositoryLocation(
                    repo));
            documentManager = navigationContext.getOrCreateDocumentManager();
            String parentDocRef = (String) req.getAttributes().get(
                    "parentdocid");
            if (parentDocRef != null) {
                parentDm = documentManager.getDocument(new IdRef(parentDocRef));
            } else {
                handleError(res,
                        "you must specify a valid document IdRef for the parent document");
                return;
            }
        } catch (ClientException e) {
            handleError(res, e);
            return;
        }

        String docTypeName = getQueryParamValue(req, DOC_TYPE, DEFAULT_DOCTYPE);
        String titleField = "dublincore:title";
        String title = getQueryParamValue(req, titleField, "New "
                + DEFAULT_DOCTYPE);
        String id = IdUtils.generateId(title);

        try {
            DocumentModel newDm = documentManager.createDocumentModel(
                    parentDm.getPathAsString(), id, docTypeName);
            Form queryParameters = req.getResourceRef().getQueryAsForm();
            for (String paramName : queryParameters.getNames()) {
                if (!DOC_TYPE.equals(paramName)) {<commit_msg>make CreateDocumentRestlet build a more meaningful default title<commit_after>/*
 * (C) Copyright 2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     Nuxeo - initial API and implementation
 *
 * $Id: CreateDocumentRestlet.java 30586 2008-02-26 14:30:17Z ogrisel $
 */

package org.nuxeo.ecm.platform.ui.web.restAPI;

import static org.jboss.seam.ScopeType.EVENT;

import org.dom4j.Element;
import org.dom4j.dom.DOMDocument;
import org.dom4j.dom.DOMDocumentFactory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.nuxeo.common.utils.IdUtils;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.IdRef;
import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
import org.nuxeo.ecm.platform.ui.web.tag.fn.LiveEditConstants;
import org.nuxeo.ecm.platform.util.RepositoryLocation;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;

/**
 * Allow the creation of a new document of the specified document type
 *
 * @author <NAME> <<EMAIL>>
 */
@Name("createDocumentRestlet")
@Scope(EVENT)
public class CreateDocumentRestlet extends BaseNuxeoRestlet implements
        LiveEditConstants {

    @In(create = true)
    protected NavigationContext navigationContext;

    protected CoreSession documentManager;

    @Override
    public void handle(Request req, Response res) {
        String repo = (String) req.getAttributes().get("repo");
        if (repo == null || repo.equals("*")) {
            handleError(res, "you must specify a repository");
            return;
        }

        DocumentModel parentDm = null;
        try {
            navigationContext.setCurrentServerLocation(new RepositoryLocation(
                    repo));
            documentManager = navigationContext.getOrCreateDocumentManager();
            String parentDocRef = (String) req.getAttributes().get(
                    "parentdocid");
            if (parentDocRef != null) {
                parentDm = documentManager.getDocument(new IdRef(parentDocRef));
            } else {
                handleError(res,
                        "you must specify a valid document IdRef for the parent document");
                return;
            }
        } catch (ClientException e) {
            handleError(res, e);
            return;
        }

        String docTypeName = getQueryParamValue(req, DOC_TYPE, DEFAULT_DOCTYPE);
        String titleField = "dublincore:title";
        String title = getQueryParamValue(req, titleField, "New " + docTypeName);
        String id = IdUtils.generateId(title);

        try {
            DocumentModel newDm = documentManager.createDocumentModel(
                    parentDm.getPathAsString(), id, docTypeName);
            Form queryParameters = req.getResourceRef().getQueryAsForm();
            for (String paramName : queryParameters.getNames()) {
                if (!DOC_TYPE.equals(paramName)) {
{'fraction_non_alphanumeric': 0.09619348632678301, 'fraction_numerical': 0.007695478906142641, 'mean_word_length': 2.9640522875816995, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2376400', 'n_tokens_mistral': 2120, 'n_tokens_neox': 2000, 'n_words': 531}