Coverage Report - com.thindeck.dynamo.DyDeck
 
Classes in this File Line Coverage Branch Coverage Complexity
DyDeck
0%
0/23
0%
0/34
1.8
 
 1  0
 /**
 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.dynamo;
 31  
 
 32  
 import com.amazonaws.services.dynamodbv2.model.Select;
 33  
 import com.google.common.collect.Iterables;
 34  
 import com.jcabi.aspects.Immutable;
 35  
 import com.jcabi.dynamo.AttributeUpdates;
 36  
 import com.jcabi.dynamo.Item;
 37  
 import com.jcabi.dynamo.QueryValve;
 38  
 import com.jcabi.dynamo.Region;
 39  
 import com.jcabi.xml.StrictXML;
 40  
 import com.jcabi.xml.XML;
 41  
 import com.jcabi.xml.XMLDocument;
 42  
 import com.jcabi.xml.XSL;
 43  
 import com.jcabi.xml.XSLDocument;
 44  
 import com.thindeck.api.Agent;
 45  
 import com.thindeck.api.Deck;
 46  
 import com.thindeck.api.Events;
 47  
 import java.io.IOException;
 48  
 import java.util.Iterator;
 49  
 import lombok.EqualsAndHashCode;
 50  
 import lombok.ToString;
 51  
 import org.xembly.Directive;
 52  
 import org.xembly.Xembler;
 53  
 
 54  
 /**
 55  
  * Dynamo implementation of {@link com.thindeck.api.Deck}.
 56  
  *
 57  
  * @author Krzysztof Krason (Krzysztof.Krason@gmail.com)
 58  
  * @author Yegor Bugayenko (yegor@teamed.io)
 59  
  * @version $Id$
 60  
  */
 61  0
 @ToString
 62  
 @Immutable
 63  0
 @EqualsAndHashCode(of = { "region", "user", "deck" })
 64  
 final class DyDeck implements Deck {
 65  
 
 66  
     /**
 67  
      * Table name.
 68  
      */
 69  
     public static final String TBL = "decks";
 70  
 
 71  
     /**
 72  
      * Name of the deck owner.
 73  
      */
 74  
     public static final String HASH = "user";
 75  
 
 76  
     /**
 77  
      * Unique name of the deck, for that user.
 78  
      */
 79  
     public static final String RANGE = "name";
 80  
 
 81  
     /**
 82  
      * When updated.
 83  
      */
 84  
     public static final String ATTR_UPDATED = "updated";
 85  
 
 86  
     /**
 87  
      * Deck.
 88  
      */
 89  
     public static final String ATTR_MEMO = "deck";
 90  
 
 91  
     /**
 92  
      * Clean-up XSL.
 93  
      */
 94  0
     private static final XSL CLEANUP = XSLDocument.make(
 95  
         DyDeck.class.getResourceAsStream("cleanup.xsl")
 96  
     );
 97  
 
 98  
     /**
 99  
      * Region.
 100  
      */
 101  
     private final transient Region region;
 102  
 
 103  
     /**
 104  
      * Name of the owner.
 105  
      */
 106  
     private final transient String user;
 107  
 
 108  
     /**
 109  
      * Name of the deck.
 110  
      */
 111  
     private final transient String deck;
 112  
 
 113  
     /**
 114  
      * Ctor.
 115  
      * @param reg Region
 116  
      * @param owner Name of the owner
 117  
      * @param name Deck name
 118  
      */
 119  0
     DyDeck(final Region reg, final String owner, final String name) {
 120  0
         this.region = reg;
 121  0
         this.user = owner;
 122  0
         this.deck = name;
 123  0
     }
 124  
 
 125  
     @Override
 126  
     public String name() {
 127  0
         return this.deck;
 128  
     }
 129  
 
 130  
     @Override
 131  
     public void exec(final Agent agent) throws IOException {
 132  0
         final String text = this.item().get(DyDeck.ATTR_MEMO).getS();
 133  0
         final XML xml = new StrictXML(
 134  
             Deck.UPGRADE.transform(
 135  
                 DyDeck.CLEANUP.transform(new XMLDocument(text))
 136  
             ),
 137  
             Deck.SCHEMA
 138  
         );
 139  0
         final Iterable<Directive> dirs = agent.exec(xml);
 140  0
         if (!Iterables.isEmpty(dirs)) {
 141  0
             final String update = new StrictXML(
 142  
                 new XMLDocument(
 143  
                     new Xembler(dirs).applyQuietly(xml.node())
 144  
                 ),
 145  
                 Deck.SCHEMA
 146  
             ).toString();
 147  0
             if (!text.equals(update)) {
 148  0
                 this.item().put(
 149  
                     new AttributeUpdates().with(
 150  
                         DyDeck.ATTR_MEMO,
 151  
                         update
 152  
                     )
 153  
                 );
 154  
             }
 155  
         }
 156  0
     }
 157  
 
 158  
     @Override
 159  
     public Events events() {
 160  0
         return new DyEvents(this.region, this.name());
 161  
     }
 162  
 
 163  
     /**
 164  
      * Item.
 165  
      * @return Item
 166  
      */
 167  
     private Item item() {
 168  0
         final Iterator<Item> items = this.region
 169  
             .table(DyDeck.TBL)
 170  
             .frame()
 171  
             .through(
 172  
                 new QueryValve()
 173  
                     .withSelect(Select.ALL_ATTRIBUTES)
 174  
                     .withLimit(1)
 175  
             )
 176  
             .where(DyDeck.HASH, this.user)
 177  
             .where(DyDeck.RANGE, this.deck)
 178  
             .iterator();
 179  0
         if (!items.hasNext()) {
 180  0
             throw new IllegalArgumentException(
 181  
                 String.format("deck '%s' not found", this.deck)
 182  
             );
 183  
         }
 184  0
         return items.next();
 185  
     }
 186  
 }