001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.webengine.model;
021
022import javax.ws.rs.core.MediaType;
023
024import org.nuxeo.ecm.webengine.scripting.ScriptFile;
025
026/**
027 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
028 */
029public class View extends Template {
030
031    protected String name;
032
033    protected String ext;
034
035    protected MediaType mediaType;
036
037    protected View(WebContext ctx, Resource resource, String name) {
038        super(ctx, resource, null);
039        mediaType = ctx.getHttpHeaders().getMediaType();
040        this.name = name;
041    }
042
043    public View(WebContext ctx, String name) {
044        this(ctx, null, name);
045    }
046
047    public View(Resource resource, String name) {
048        this(resource.getContext(), resource, name);
049    }
050
051    public View mediaType(MediaType mediaType) {
052        this.mediaType = mediaType;
053        return this;
054    }
055
056    public View name(String name) {
057        this.name = name;
058        return this;
059    }
060
061    public View extension(String ext) {
062        this.ext = ext;
063        return this;
064    }
065
066    public MediaType mediaType() {
067        return mediaType;
068    }
069
070    public View fileName(String fileName) {
071        resolve(fileName);
072        return this;
073    }
074
075    public View script(ScriptFile script) {
076        this.script = script;
077        return this;
078    }
079
080    @Override
081    public ScriptFile script() {
082        if (script == null) {
083            StringBuilder fileName = new StringBuilder();
084            fileName.append(name);
085            if (mediaType != null) {
086                String mediaId = ctx.getModule().getMediaTypeId(mediaType);
087                if (mediaId != null) {
088                    fileName.append('-').append(mediaId);
089                }
090            }
091            if (ext == null) {
092                ext = ctx.getModule().getTemplateFileExt();
093            }
094            fileName.append('.').append(ext);
095            resolve(fileName.toString());
096        }
097        return script;
098    }
099
100    public View resolve() {
101        script(); // resolve script
102        return this;
103    }
104
105}