summaryrefslogtreecommitdiffstats
path: root/src/commands/updateentries.cpp
blob: 6b531027ed5395e9bffad02eb78668fed6fb7d20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/***************************************************************************
    copyright            : (C) 2006 by Robby Stephenson
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of version 2 of the GNU General Public License as  *
 *   published by the Free Software Foundation;                            *
 *                                                                         *
 ***************************************************************************/

#include "updateentries.h"
#include "fieldcommand.h"
#include "modifyentries.h"
#include "../collection.h"
#include "../tellico_kernel.h"
#include "../document.h"

#include <klocale.h>

using Tellico::Command::UpdateEntries;

namespace Tellico {
  namespace Command {

class MergeEntries : public KCommand {
public:
  MergeEntries(Data::EntryPtr currEntry_, Data::EntryPtr newEntry_, bool overWrite_) : KCommand()
    , m_oldEntry(new Data::Entry(*currEntry_)) {
    // we merge the entries here instead of in execute() because this
    // command is never called without also calling ModifyEntries()
    // which takes care of copying the entry values
    Data::Collection::mergeEntry(currEntry_, newEntry_, overWrite_);
  }

  virtual void execute() {} // does nothing
  virtual void unexecute() {} // does nothing
  virtual TQString name() const { return TQString(); }
  Data::EntryPtr oldEntry() const { return m_oldEntry; }

private:
  Data::EntryPtr m_oldEntry;
};
  }
}

UpdateEntries::UpdateEntries(Data::CollPtr coll_, Data::EntryPtr oldEntry_, Data::EntryPtr newEntry_, bool overWrite_)
    : Group(i18n("Modify (Entry Title)", "Modify %1").tqarg(newEntry_->title()))
    , m_coll(coll_)
    , m_oldEntry(oldEntry_)
    , m_newEntry(newEntry_)
    , m_overWrite(overWrite_)
{
}

void UpdateEntries::execute() {
  if(isEmpty()) {
    // add commands
    // do this here instead of the constructor because several UpdateEntries may be in one command
    // and I don't want to add new fields multiple times

    TQPair<Data::FieldVec, Data::FieldVec> p = Kernel::self()->mergeFields(m_coll,
                                                                          m_newEntry->collection()->fields(),
                                                                          m_newEntry);
    Data::FieldVec modifiedFields = p.first;
    Data::FieldVec addedFields = p.second;

    for(Data::FieldVec::Iterator field = modifiedFields.begin(); field != modifiedFields.end(); ++field) {
      if(m_coll->hasField(field->name())) {
        addCommand(new FieldCommand(FieldCommand::FieldModify, m_coll,
                                    field, m_coll->fieldByName(field->name())));
      }
    }

    for(Data::FieldVec::Iterator field = addedFields.begin(); field != addedFields.end(); ++field) {
      addCommand(new FieldCommand(FieldCommand::FieldAdd, m_coll, field));
    }

    // MergeEntries copies values from m_newEntry into m_oldEntry
    // m_oldEntry is in the current collection
    // m_newEntry isn't...
    MergeEntries* cmd = new MergeEntries(m_oldEntry, m_newEntry, m_overWrite);
    addCommand(cmd);
    // cmd->oldEntry() returns a copy of m_oldEntry before values were merged
    // m_oldEntry has new values
    // in the ModifyEntries command, the second entry should be owned by the current
    // collection and contain the updated values
    // the first one is not owned by current collection
    addCommand(new ModifyEntries(m_coll, cmd->oldEntry(), m_oldEntry));
  }
  Group::execute();
}