Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / commands / PasteCommand.java @ e5af9268

History | View | Annotate | Download (6.1 kB)

1
/*
2
 * Copyright 2011 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
12
 *   2. Redistributions in binary form must reproduce the above
13
 *      copyright notice, this list of conditions and the following
14
 *      disclaimer in the documentation and/or other materials
15
 *      provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * The views and conclusions contained in the software and
31
 * documentation are those of the authors and should not be
32
 * interpreted as representing official policies, either expressed
33
 * or implied, of GRNET S.A.
34
 */
35
package gr.grnet.pithos.web.client.commands;
36

    
37
import com.google.gwt.core.client.Scheduler;
38
import gr.grnet.pithos.web.client.Clipboard;
39
import gr.grnet.pithos.web.client.Pithos;
40
import gr.grnet.pithos.web.client.foldertree.File;
41
import gr.grnet.pithos.web.client.foldertree.Folder;
42
import gr.grnet.pithos.web.client.foldertree.Resource;
43
import gr.grnet.pithos.web.client.rest.PutRequest;
44
import gr.grnet.pithos.web.client.rest.RestException;
45

    
46
import java.util.Iterator;
47
import java.util.List;
48

    
49
import com.google.gwt.core.client.GWT;
50
import com.google.gwt.http.client.Response;
51
import com.google.gwt.user.client.Command;
52
import com.google.gwt.user.client.ui.PopupPanel;
53

    
54
public class PasteCommand implements Command {
55

    
56
        protected Pithos app;
57
        private PopupPanel containerPanel;
58
        protected Folder folder;
59

    
60
        public PasteCommand(Pithos _app, PopupPanel _containerPanel, Folder _folder) {
61
        app = _app;
62
                containerPanel = _containerPanel;
63
        folder = _folder;
64
        }
65

    
66
        @Override
67
        public void execute() {
68
        if (containerPanel != null)
69
                    containerPanel.hide();
70

    
71
        Object clipboardItem = app.getClipboard().getItem();
72
        if (clipboardItem == null)
73
            return;
74
        int operation = app.getClipboard().getOperation();
75
        if (clipboardItem instanceof Folder) {
76
            final Folder tobeCopied = (Folder) clipboardItem;
77
            if (operation == Clipboard.COPY) {
78
                app.copyFolder(tobeCopied, folder.getOwner(), folder.getUri(), new Command() {
79
                    @Override
80
                    public void execute() {
81
                        app.getClipboard().clear();
82
                        app.updateFolder(folder, true, new Command() {
83
                                                        
84
                                                        @Override
85
                                                        public void execute() {
86
                                                                app.updateStatistics();
87
                                                        }
88
                                                });
89
                    }
90
                });
91
            }
92
            else {
93
                app.copyFolder(tobeCopied, folder.getOwner(), folder.getUri(), new Command() {
94
                    @Override
95
                    public void execute() {
96
                        app.getClipboard().clear();
97
                        app.deleteFolder(tobeCopied);
98
                        app.updateFolder(folder, true, null);
99
                    }
100
                });
101
            }
102
        }
103
        else {
104
            @SuppressWarnings("unchecked")
105
                        List<File> tobeCopied = (List<File>) clipboardItem;
106
            Iterator<File> iter = tobeCopied.iterator();
107
            if (operation == Clipboard.COPY) {
108
                app.copyFiles(iter, folder.getOwner(), folder.getUri(), new Command() {
109
                    @Override
110
                    public void execute() {
111
                        app.getClipboard().clear();
112
                        app.updateFolder(folder, true, new Command() {
113
                                                        
114
                                                        @Override
115
                                                        public void execute() {
116
                                                                app.updateStatistics();
117
                                                        }
118
                                                });
119
                    }
120
                });
121
            }
122
            else {
123
                moveFiles(iter, new Command() {
124
                    @Override
125
                    public void execute() {
126
                        app.getClipboard().clear();
127
                        app.updateFolder(folder, true, null);
128
                    }
129
                });
130
            }
131
        }
132
        }
133

    
134
        protected void moveFiles(final Iterator<File> iter, final Command callback) {
135
        if (iter.hasNext()) {
136
            File file = iter.next();
137
            String path = folder.getUri() + "/" + file.getName();
138
            PutRequest copyFile = new PutRequest(app.getApiPath(), folder.getOwner(), path) {
139
                @Override
140
                public void onSuccess(@SuppressWarnings("unused") Resource result) {
141
                    moveFiles(iter, callback);
142
                }
143

    
144
                @Override
145
                public void onError(Throwable t) {
146
                    GWT.log("", t);
147
                    if (t instanceof RestException) {
148
                        app.displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
149
                    }
150
                    else
151
                        app.displayError("System error unable to copy file: "+t.getMessage());
152
                }
153

    
154
                                @Override
155
                                protected void onUnauthorized(Response response) {
156
                                        app.sessionExpired();
157
                                }
158
            };
159
            copyFile.setHeader("X-Auth-Token", app.getToken());
160
            copyFile.setHeader("X-Move-From", file.getUri());
161
            copyFile.setHeader("Content-Type", file.getContentType());
162
            Scheduler.get().scheduleDeferred(copyFile);
163
        }
164
        else if (callback != null) {
165
            callback.execute();
166
        }
167
    }
168
}