001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.webengine.model;
023
024import javax.ws.rs.core.MediaType;
025
026import org.nuxeo.ecm.webengine.scripting.ScriptFile;
027
028/**
029 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
030 */
031public class View extends Template {
032
033    protected String name;
034
035    protected String ext;
036
037    protected MediaType mediaType;
038
039    protected View(WebContext ctx, Resource resource, String name) {
040        super(ctx, resource, null);
041        mediaType = ctx.getHttpHeaders().getMediaType();
042        this.name = name;
043    }
044
045    public View(WebContext ctx, String name) {
046        this(ctx, null, name);
047    }
048
049    public View(Resource resource, String name) {
050        this(resource.getContext(), resource, name);
051    }
052
053    public View mediaType(MediaType mediaType) {
054        this.mediaType = mediaType;
055        return this;
056    }
057
058    public View name(String name) {
059        this.name = name;
060        return this;
061    }
062
063    public View extension(String ext) {
064        this.ext = ext;
065        return this;
066    }
067
068    public MediaType mediaType() {
069        return mediaType;
070    }
071
072    public View fileName(String fileName) {
073        resolve(fileName);
074        return this;
075    }
076
077    public View script(ScriptFile script) {
078        this.script = script;
079        return this;
080    }
081
082    @Override
083    public ScriptFile script() {
084        if (script == null) {
085            StringBuilder fileName = new StringBuilder();
086            fileName.append(name);
087            if (mediaType != null) {
088                String mediaId = ctx.getModule().getMediaTypeId(mediaType);
089                if (mediaId != null) {
090                    fileName.append('-').append(mediaId);
091                }
092            }
093            if (ext == null) {
094                ext = ctx.getModule().getTemplateFileExt();
095            }
096            fileName.append('.').append(ext);
097            resolve(fileName.toString());
098        }
099        return script;
100    }
101
102    public View resolve() {
103        script(); // resolve script
104        return this;
105    }
106
107}