Coverage Report - com.thindeck.cockpit.deck.TkIndex
 
Classes in this File Line Coverage Branch Coverage Complexity
TkIndex
42%
3/7
N/A
1
TkIndex$1
0%
0/4
N/A
1
 
 1  
 /**
 2  
  * Copyright (c) 2014-2015, Thindeck.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the thindeck.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.thindeck.cockpit.deck;
 31  
 
 32  
 import com.google.common.collect.Iterables;
 33  
 import com.jcabi.aspects.Tv;
 34  
 import com.thindeck.api.Base;
 35  
 import com.thindeck.api.Deck;
 36  
 import com.thindeck.cockpit.RsPage;
 37  
 import java.io.IOException;
 38  
 import java.util.Date;
 39  
 import org.ocpsoft.prettytime.PrettyTime;
 40  
 import org.takes.Request;
 41  
 import org.takes.Response;
 42  
 import org.takes.Take;
 43  
 import org.takes.misc.Href;
 44  
 import org.takes.rs.xe.XeAppend;
 45  
 import org.takes.rs.xe.XeChain;
 46  
 import org.takes.rs.xe.XeDirectives;
 47  
 import org.takes.rs.xe.XeLink;
 48  
 import org.takes.rs.xe.XeSource;
 49  
 import org.takes.rs.xe.XeTransform;
 50  
 import org.xembly.Directives;
 51  
 import org.xembly.Xembler;
 52  
 
 53  
 /**
 54  
  * Deck.
 55  
  *
 56  
  * @author Yegor Bugayenko (yegor@teamed.io)
 57  
  * @version $Id$
 58  
  * @since 0.5
 59  
  * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
 60  
  * @checkstyle MultipleStringLiteralsCheck (500 lines)
 61  
  */
 62  
 public final class TkIndex implements Take {
 63  
 
 64  
     /**
 65  
      * Base.
 66  
      */
 67  
     private final transient Base base;
 68  
 
 69  
     /**
 70  
      * Ctor.
 71  
      * @param bse Base
 72  
      */
 73  1
     TkIndex(final Base bse) {
 74  1
         this.base = bse;
 75  1
     }
 76  
 
 77  
     @Override
 78  
     public Response act(final Request req) throws IOException {
 79  0
         final Deck deck = new RqDeck(this.base, req).deck();
 80  0
         final Href home = new Href("/d").path(deck.name());
 81  0
         final PrettyTime pretty = new PrettyTime();
 82  0
         return new RsPage(
 83  
             "/xsl/deck.xsl",
 84  
             this.base,
 85  
             req,
 86  
             new XeAppend(
 87  
                 "deck",
 88  
                 new XeDirectives(
 89  
                     Directives.copyOf(new Deck.Smart(deck).xml().node())
 90  
                 ),
 91  
                 new XeChain(
 92  
                     new XeLink("open", home.path("open")),
 93  
                     new XeLink("help", home.path("help")),
 94  
                     new XeLink("command", home.path("command"))
 95  
                 )
 96  
             ),
 97  
             new XeAppend(
 98  
                 "events",
 99  
                 new XeTransform<>(
 100  
                     Iterables.limit(
 101  
                         deck.events().iterate(Long.MAX_VALUE),
 102  
                         Tv.TWENTY
 103  
                     ),
 104  0
                     new XeTransform.Func<String>() {
 105  
                         @Override
 106  
                         public XeSource transform(final String txt) {
 107  0
                             final String[] parts = txt.split("\n", Tv.THREE);
 108  0
                             final long msec = Long.parseLong(parts[1]);
 109  0
                             return new XeDirectives(
 110  
                                 new Directives().add("event")
 111  
                                     .attr("head", Xembler.escape(parts[0]))
 112  
                                     .attr("msec", Long.toString(msec))
 113  
                                     .attr("ago", pretty.format(new Date(msec)))
 114  
                                     .set(Xembler.escape(parts[2]))
 115  
                             );
 116  
                         }
 117  
                     }
 118  
                 )
 119  
             )
 120  
         );
 121  
     }
 122  
 
 123  
 }