| 1 | //  Copyright 2004-2007 Jean-Francois Poilpret | 
| 2 | // | 
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
| 4 | // you may not use this file except in compliance with the License. | 
| 5 | // You may obtain a copy of the License at | 
| 6 | // | 
| 7 | //     http://www.apache.org/licenses/LICENSE-2.0 | 
| 8 | // | 
| 9 | // Unless required by applicable law or agreed to in writing, software | 
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, | 
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| 12 | // See the License for the specific language governing permissions and | 
| 13 | // limitations under the License. | 
| 14 |   | 
| 15 | package net.sourceforge.hiveboard.business; | 
| 16 |   | 
| 17 | import java.awt.image.BufferedImage; | 
| 18 | import java.util.Arrays; | 
| 19 |   | 
| 20 | import org.apache.commons.logging.Log; | 
| 21 | import org.apache.commons.logging.LogFactory; | 
| 22 |   | 
| 23 | import net.sf.ehcache.Cache; | 
| 24 |   | 
| 25 | import net.sourceforge.hiveboard.Board; | 
| 26 | import net.sourceforge.hiveboard.dao.BoardDAO; | 
| 27 | import net.sourceforge.hiveboard.drawing.ClearBoardAction; | 
| 28 | import net.sourceforge.hiveboard.drawing.CompoundHiliteAction; | 
| 29 | import net.sourceforge.hiveboard.drawing.DrawingAction; | 
| 30 | import net.sourceforge.hiveboard.util.ImageCache; | 
| 31 | import net.sourceforge.hiveboard.util.ImageDef; | 
| 32 | import net.sourceforge.hiveboard.util.ImageHolder; | 
| 33 | import net.sourceforge.hiveboard.util.RGBImageHolder; | 
| 34 | import net.sourceforge.hiveutils.collections.Queue; | 
| 35 | import net.sourceforge.hiveutils.collections.impl.QueueImpl; | 
| 36 | import net.sourceforge.hiveutils.service.ObjectTools; | 
| 37 |   | 
| 38 | /** | 
| 39 |  * @author Jean-Francois Poilpret | 
| 40 |  */ | 
| 41 | public class BoardImageHolderImpl extends ImageCache implements BoardImageHolder | 
| 42 | { | 
| 43 |         static private final Log        _logger = LogFactory.getLog(BoardImageHolderImpl.class); | 
| 44 |          | 
| 45 |         public BoardImageHolderImpl(BoardDAO        boardDAO, | 
| 46 |                                                                 ObjectTools        objectTools, | 
| 47 |                                                                 Cache                cache, | 
| 48 |                                                                 Board                board) | 
| 49 |         { | 
| 50 |                 super(cache, board.getId()); | 
| 51 |                 _boardDAO = boardDAO; | 
| 52 |                 _objectTools = objectTools; | 
| 53 |                 _board = board; | 
| 54 |                 _idSnapshot = _boardDAO.selectCurrentSnapshot(_board.getId()); | 
| 55 |                 if (_idSnapshot == -1) | 
| 56 |                 { | 
| 57 |                         _logger.debug("<init> insert snapshot for board <" + _board.getId() + ">"); | 
| 58 |                         byte[] raster = | 
| 59 |                                 new byte[_board.getWidth() * _board.getHeight() * ImageDef.BYTES_PER_PIXEL]; | 
| 60 |                         // Force white background | 
| 61 |                         Arrays.fill(raster, PIXEL_DEFAULT); | 
| 62 |                         ImageHolder holder = new RGBImageHolder(); | 
| 63 |                         holder.setImage(_board.getWidth(), _board.getHeight(), raster); | 
| 64 |                         // Save to cache immediately | 
| 65 |                         writeCache(holder); | 
| 66 |                         byte[] content = _objectTools.compress(raster); | 
| 67 |                         _idSnapshot = _boardDAO.insertSnapshot(_board.getId(), true, content); | 
| 68 |                 } | 
| 69 |         } | 
| 70 |          | 
| 71 |         synchronized public ImageInfo        getImage() | 
| 72 |         { | 
| 73 |                 _logger.debug("getImage() #1 for board " + _board.getId()); | 
| 74 |                 boolean mustWriteCache = false; | 
| 75 |                 // Rebuild the image from DB content | 
| 76 |                 ImageHolder holder = readCache(); | 
| 77 |                 if (holder == null) | 
| 78 |                 { | 
| 79 |                         _logger.debug("getImage() #2"); | 
| 80 |                         byte[] raster = _boardDAO.selectSnapshot(_idSnapshot).getContent(); | 
| 81 |                         raster = _objectTools.uncompress(raster); | 
| 82 |                         holder = new RGBImageHolder(); | 
| 83 |                         holder.setImage(_board.getWidth(), _board.getHeight(), raster); | 
| 84 |                         mustWriteCache = true; | 
| 85 |                 } | 
| 86 |                 if (!_actions.isEmpty()) | 
| 87 |                 { | 
| 88 |                         _logger.debug("getImage() #3"); | 
| 89 |                         updateImage(holder); | 
| 90 |                         mustWriteCache = true; | 
| 91 |                 } | 
| 92 |                 if (mustWriteCache) | 
| 93 |                 { | 
| 94 |                         _logger.debug("getImage() #4"); | 
| 95 |                         writeCache(holder); | 
| 96 |                 } | 
| 97 |                 byte[] action = null; | 
| 98 |                 if (!_hiliteActions.isEmpty()) | 
| 99 |                 { | 
| 100 |                         _logger.debug("getImage() #5"); | 
| 101 |                         action = _objectTools.serialize(_hiliteActions); | 
| 102 |                 } | 
| 103 |                 _logger.debug("getImage() #6"); | 
| 104 |                 ImageInfo info = new ImageInfo(holder.getImageRaster(), action); | 
| 105 |                 _logger.debug("getImage() #7 end"); | 
| 106 |                 return info; | 
| 107 |         } | 
| 108 |          | 
| 109 |         private void        updateImage(ImageHolder holder) | 
| 110 |         { | 
| 111 |                 // First apply actions to image | 
| 112 |                 BufferedImage image = holder.getImage(); | 
| 113 |                 for (DrawingAction action: _actions.take()) | 
| 114 |                 { | 
| 115 |                         action.perform(image); | 
| 116 |                 } | 
| 117 |                 holder.setImage(image); | 
| 118 |                 // Then save image raster to DB | 
| 119 |                 byte[] raster = holder.getImageRaster(); | 
| 120 |                 byte[] content = _objectTools.compress(raster); | 
| 121 |                 _boardDAO.updateSnapshot(_idSnapshot, content); | 
| 122 |         } | 
| 123 |          | 
| 124 |         synchronized public void        save() | 
| 125 |         { | 
| 126 |                 // Rebuild the image from DB content (only if necessary) | 
| 127 |                 if (!_actions.isEmpty()) | 
| 128 |                 { | 
| 129 |                         getImage(); | 
| 130 |                 } | 
| 131 |         } | 
| 132 |   | 
| 133 |         synchronized public void        dispose() | 
| 134 |         { | 
| 135 |                 removeCache(); | 
| 136 |         } | 
| 137 |          | 
| 138 |         synchronized public void        addDrawingAction(DrawingAction action) | 
| 139 |         { | 
| 140 |                 // Optimization when board is cleared => remvoe all previous actions first | 
| 141 |                 if (action instanceof ClearBoardAction && !_actions.isEmpty()) | 
| 142 |                 { | 
| 143 |                         _actions.take(); | 
| 144 |                 } | 
| 145 |                 if (!_hiliteActions.addAction(action)) | 
| 146 |                 { | 
| 147 |                         _hiliteActions.clear(); | 
| 148 |                         _actions.add(action); | 
| 149 |                 } | 
| 150 |         } | 
| 151 |          | 
| 152 |         static final private byte                        PIXEL_DEFAULT = (byte) 0xFF; | 
| 153 |          | 
| 154 |         private final BoardDAO                                _boardDAO; | 
| 155 |         private final ObjectTools                        _objectTools; | 
| 156 |         private final Board                                        _board; | 
| 157 |         private int                                                        _idSnapshot; | 
| 158 |         private final Queue<DrawingAction>        _actions = new QueueImpl<DrawingAction>(); | 
| 159 |         private final CompoundHiliteAction        _hiliteActions = new CompoundHiliteAction(); | 
| 160 | } |