View Javadoc

1   /*
2    * Copyright 2004 Carlos Sanchez.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.oness.party.model.facade.action;
17  
18  import java.util.List;
19  
20  import net.sf.oness.common.model.dao.Dao;
21  import net.sf.oness.common.model.exceptions.ExistingInstanceException;
22  import net.sf.oness.party.model.party.bo.Party;
23  
24  /***
25   * @author Carlos Sanchez
26   * @version $Revision: 1.6 $
27   */
28  public class CreatePartyAction {
29  
30      private Party party;
31  
32      private Dao partyDao;
33  
34      private boolean ignoreExisting;
35  
36      public CreatePartyAction(Dao partyDao, Party party, boolean ignoreExisting) {
37          this.partyDao = partyDao;
38          this.party = party;
39          this.ignoreExisting = ignoreExisting;
40      }
41  
42      public Party execute() throws ExistingInstanceException {
43          if (party.getInternalName() == null)
44              throw new NullPointerException();
45  
46          if (!ignoreExisting) {
47              Party partyCriteria = new Party();
48              partyCriteria.setInternalName(party.getInternalName());
49              List existing = partyDao.find(partyCriteria, 0, Integer.MAX_VALUE);
50              if (existing.size() > 0) {
51                  throw new ExistingInstanceException(party, party.getClass()
52                          .getName(), existing);
53              }
54          }
55          return (Party) partyDao.create(party);
56      }
57  }