PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/portal-impl/src/com/liferay/portal/service/impl/CompanyLocalServiceImpl.java

https://github.com/viktorkovacs/liferay-portal-trunk
Java | 861 lines | 609 code | 213 blank | 39 comment | 50 complexity | 3335adae6cdc304768d6c0acdc79aa39 MD5 | raw file
  1. /**
  2. * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.portal.service.impl;
  15. import com.liferay.portal.AccountNameException;
  16. import com.liferay.portal.CompanyMxException;
  17. import com.liferay.portal.CompanyVirtualHostException;
  18. import com.liferay.portal.CompanyWebIdException;
  19. import com.liferay.portal.NoSuchShardException;
  20. import com.liferay.portal.NoSuchUserException;
  21. import com.liferay.portal.NoSuchVirtualHostException;
  22. import com.liferay.portal.kernel.exception.PortalException;
  23. import com.liferay.portal.kernel.exception.SystemException;
  24. import com.liferay.portal.kernel.language.LanguageUtil;
  25. import com.liferay.portal.kernel.search.BooleanClauseOccur;
  26. import com.liferay.portal.kernel.search.BooleanQuery;
  27. import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
  28. import com.liferay.portal.kernel.search.Field;
  29. import com.liferay.portal.kernel.search.Hits;
  30. import com.liferay.portal.kernel.search.SearchEngineUtil;
  31. import com.liferay.portal.kernel.util.LocaleUtil;
  32. import com.liferay.portal.kernel.util.PropsKeys;
  33. import com.liferay.portal.kernel.util.StringPool;
  34. import com.liferay.portal.kernel.util.TimeZoneUtil;
  35. import com.liferay.portal.kernel.util.UnicodeProperties;
  36. import com.liferay.portal.kernel.util.Validator;
  37. import com.liferay.portal.kernel.workflow.WorkflowConstants;
  38. import com.liferay.portal.model.Account;
  39. import com.liferay.portal.model.Company;
  40. import com.liferay.portal.model.CompanyConstants;
  41. import com.liferay.portal.model.Contact;
  42. import com.liferay.portal.model.ContactConstants;
  43. import com.liferay.portal.model.Group;
  44. import com.liferay.portal.model.GroupConstants;
  45. import com.liferay.portal.model.Role;
  46. import com.liferay.portal.model.RoleConstants;
  47. import com.liferay.portal.model.User;
  48. import com.liferay.portal.model.VirtualHost;
  49. import com.liferay.portal.service.ServiceContext;
  50. import com.liferay.portal.service.base.CompanyLocalServiceBaseImpl;
  51. import com.liferay.portal.util.Portal;
  52. import com.liferay.portal.util.PrefsPropsUtil;
  53. import com.liferay.portal.util.PropsUtil;
  54. import com.liferay.portal.util.PropsValues;
  55. import com.liferay.util.Encryptor;
  56. import com.liferay.util.EncryptorException;
  57. import java.io.File;
  58. import java.io.IOException;
  59. import java.io.InputStream;
  60. import java.util.ArrayList;
  61. import java.util.Calendar;
  62. import java.util.Date;
  63. import java.util.List;
  64. import java.util.Locale;
  65. import java.util.Map;
  66. import javax.portlet.PortletException;
  67. import javax.portlet.PortletPreferences;
  68. /**
  69. * @author Brian Wing Shun Chan
  70. * @author Julio Camarero
  71. */
  72. public class CompanyLocalServiceImpl extends CompanyLocalServiceBaseImpl {
  73. public Company addCompany(
  74. String webId, String virtualHostname, String mx, String shardName,
  75. boolean system, int maxUsers)
  76. throws PortalException, SystemException {
  77. // Company
  78. virtualHostname = virtualHostname.trim().toLowerCase();
  79. if ((Validator.isNull(webId)) ||
  80. (webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) ||
  81. (companyPersistence.fetchByWebId(webId) != null)) {
  82. throw new CompanyWebIdException();
  83. }
  84. validate(webId, virtualHostname, mx);
  85. Company company = checkCompany(webId, mx, shardName);
  86. company.setMx(mx);
  87. company.setSystem(system);
  88. company.setMaxUsers(maxUsers);
  89. companyPersistence.update(company, false);
  90. // Virtual host
  91. updateVirtualHost(company.getCompanyId(), virtualHostname);
  92. return company;
  93. }
  94. public Company checkCompany(String webId)
  95. throws PortalException, SystemException {
  96. String mx = webId;
  97. return companyLocalService.checkCompany(
  98. webId, mx, PropsValues.SHARD_DEFAULT_NAME);
  99. }
  100. public Company checkCompany(String webId, String mx, String shardName)
  101. throws PortalException, SystemException {
  102. // Company
  103. Date now = new Date();
  104. Company company = companyPersistence.fetchByWebId(webId);
  105. if (company == null) {
  106. String virtualHostname = webId;
  107. if (webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
  108. virtualHostname = _DEFAULT_VIRTUAL_HOST;
  109. }
  110. String homeURL = null;
  111. String name = webId;
  112. String legalName = null;
  113. String legalId = null;
  114. String legalType = null;
  115. String sicCode = null;
  116. String tickerSymbol = null;
  117. String industry = null;
  118. String type = null;
  119. String size = null;
  120. long companyId = counterLocalService.increment();
  121. company = companyPersistence.create(companyId);
  122. try {
  123. company.setKeyObj(Encryptor.generateKey());
  124. }
  125. catch (EncryptorException ee) {
  126. throw new SystemException(ee);
  127. }
  128. company.setWebId(webId);
  129. company.setMx(mx);
  130. companyPersistence.update(company, false);
  131. // Shard
  132. shardLocalService.addShard(
  133. Company.class.getName(), companyId, shardName);
  134. // Company
  135. updateCompany(
  136. companyId, virtualHostname, mx, homeURL, name, legalName,
  137. legalId, legalType, sicCode, tickerSymbol, industry, type,
  138. size);
  139. // Virtual host
  140. updateVirtualHost(companyId, virtualHostname);
  141. // Demo settings
  142. if (webId.equals("liferay.net")) {
  143. company = companyPersistence.findByWebId(webId);
  144. updateVirtualHost(companyId, "demo.liferay.net");
  145. updateSecurity(
  146. companyId, CompanyConstants.AUTH_TYPE_EA, true, true, true,
  147. true, false, true);
  148. PortletPreferences preferences = PrefsPropsUtil.getPreferences(
  149. companyId);
  150. try {
  151. preferences.setValue(
  152. PropsKeys.ADMIN_EMAIL_FROM_NAME, "Liferay Demo");
  153. preferences.setValue(
  154. PropsKeys.ADMIN_EMAIL_FROM_ADDRESS, "test@liferay.net");
  155. preferences.store();
  156. }
  157. catch (IOException ioe) {
  158. throw new SystemException(ioe);
  159. }
  160. catch (PortletException pe) {
  161. throw new SystemException(pe);
  162. }
  163. }
  164. }
  165. else {
  166. try {
  167. shardLocalService.getShard(
  168. Company.class.getName(), company.getCompanyId());
  169. }
  170. catch (NoSuchShardException nsse) {
  171. shardLocalService.addShard(
  172. Company.class.getName(), company.getCompanyId(), shardName);
  173. }
  174. }
  175. long companyId = company.getCompanyId();
  176. // Key
  177. checkCompanyKey(companyId);
  178. // Default user
  179. User defaultUser = null;
  180. try {
  181. defaultUser = userLocalService.getDefaultUser(companyId);
  182. if (!defaultUser.isAgreedToTermsOfUse()) {
  183. defaultUser.setAgreedToTermsOfUse(true);
  184. userPersistence.update(defaultUser, false);
  185. }
  186. }
  187. catch (NoSuchUserException nsue) {
  188. long userId = counterLocalService.increment();
  189. defaultUser = userPersistence.create(userId);
  190. defaultUser.setCompanyId(companyId);
  191. defaultUser.setCreateDate(now);
  192. defaultUser.setModifiedDate(now);
  193. defaultUser.setDefaultUser(true);
  194. defaultUser.setContactId(counterLocalService.increment());
  195. defaultUser.setPassword("password");
  196. defaultUser.setScreenName(String.valueOf(defaultUser.getUserId()));
  197. defaultUser.setEmailAddress("default@" + company.getMx());
  198. defaultUser.setLanguageId(LocaleUtil.getDefault().toString());
  199. defaultUser.setTimeZoneId(TimeZoneUtil.getDefault().getID());
  200. defaultUser.setGreeting(
  201. LanguageUtil.format(
  202. defaultUser.getLocale(), "welcome-x", StringPool.BLANK,
  203. false));
  204. defaultUser.setLoginDate(now);
  205. defaultUser.setFailedLoginAttempts(0);
  206. defaultUser.setAgreedToTermsOfUse(true);
  207. defaultUser.setStatus(WorkflowConstants.STATUS_APPROVED);
  208. userPersistence.update(defaultUser, false);
  209. // Contact
  210. Contact defaultContact = contactPersistence.create(
  211. defaultUser.getContactId());
  212. defaultContact.setCompanyId(defaultUser.getCompanyId());
  213. defaultContact.setUserId(defaultUser.getUserId());
  214. defaultContact.setUserName(StringPool.BLANK);
  215. defaultContact.setCreateDate(now);
  216. defaultContact.setModifiedDate(now);
  217. defaultContact.setAccountId(company.getAccountId());
  218. defaultContact.setParentContactId(
  219. ContactConstants.DEFAULT_PARENT_CONTACT_ID);
  220. defaultContact.setFirstName(StringPool.BLANK);
  221. defaultContact.setMiddleName(StringPool.BLANK);
  222. defaultContact.setLastName(StringPool.BLANK);
  223. defaultContact.setMale(true);
  224. defaultContact.setBirthday(now);
  225. contactPersistence.update(defaultContact, false);
  226. }
  227. // System roles
  228. roleLocalService.checkSystemRoles(companyId);
  229. // System groups
  230. groupLocalService.checkSystemGroups(companyId);
  231. // Company group
  232. groupLocalService.checkCompanyGroup(companyId);
  233. // Default password policy
  234. passwordPolicyLocalService.checkDefaultPasswordPolicy(companyId);
  235. // Default user must have the Guest role
  236. Role guestRole = roleLocalService.getRole(
  237. companyId, RoleConstants.GUEST);
  238. roleLocalService.setUserRoles(
  239. defaultUser.getUserId(), new long[] {guestRole.getRoleId()});
  240. // Default admin
  241. if (userPersistence.countByCompanyId(companyId) == 1) {
  242. long creatorUserId = 0;
  243. boolean autoPassword = false;
  244. String password1 = PropsValues.DEFAULT_ADMIN_PASSWORD;
  245. String password2 = password1;
  246. boolean autoScreenName = false;
  247. String screenName = PropsValues.DEFAULT_ADMIN_SCREEN_NAME;
  248. String emailAddress =
  249. PropsValues.DEFAULT_ADMIN_EMAIL_ADDRESS_PREFIX + "@" + mx;
  250. long facebookId = 0;
  251. String openId = StringPool.BLANK;
  252. Locale locale = defaultUser.getLocale();
  253. String firstName = PropsValues.DEFAULT_ADMIN_FIRST_NAME;
  254. String middleName = PropsValues.DEFAULT_ADMIN_MIDDLE_NAME;
  255. String lastName = PropsValues.DEFAULT_ADMIN_LAST_NAME;
  256. int prefixId = 0;
  257. int suffixId = 0;
  258. boolean male = true;
  259. int birthdayMonth = Calendar.JANUARY;
  260. int birthdayDay = 1;
  261. int birthdayYear = 1970;
  262. String jobTitle = StringPool.BLANK;
  263. Group guestGroup = groupLocalService.getGroup(
  264. companyId, GroupConstants.GUEST);
  265. long[] groupIds = new long[] {guestGroup.getGroupId()};
  266. long[] organizationIds = null;
  267. Role adminRole = roleLocalService.getRole(
  268. companyId, RoleConstants.ADMINISTRATOR);
  269. Role powerUserRole = roleLocalService.getRole(
  270. companyId, RoleConstants.POWER_USER);
  271. long[] roleIds = new long[] {
  272. adminRole.getRoleId(), powerUserRole.getRoleId()
  273. };
  274. long[] userGroupIds = null;
  275. boolean sendEmail = false;
  276. ServiceContext serviceContext = new ServiceContext();
  277. User defaultAdminUser = userLocalService.addUser(
  278. creatorUserId, companyId, autoPassword, password1, password2,
  279. autoScreenName, screenName, emailAddress, facebookId, openId,
  280. locale, firstName, middleName, lastName, prefixId, suffixId,
  281. male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
  282. groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
  283. serviceContext);
  284. userLocalService.updateLastLogin(
  285. defaultAdminUser.getUserId(), defaultAdminUser.getLoginIP());
  286. }
  287. // Portlets
  288. portletLocalService.checkPortlets(companyId);
  289. return company;
  290. }
  291. public void checkCompanyKey(long companyId)
  292. throws PortalException, SystemException {
  293. Company company = companyPersistence.findByPrimaryKey(companyId);
  294. if ((Validator.isNull(company.getKey())) &&
  295. (company.getKeyObj() == null)) {
  296. try {
  297. company.setKeyObj(Encryptor.generateKey());
  298. }
  299. catch (EncryptorException ee) {
  300. throw new SystemException(ee);
  301. }
  302. companyPersistence.update(company, false);
  303. }
  304. }
  305. public void deleteLogo(long companyId)
  306. throws PortalException, SystemException {
  307. Company company = companyPersistence.findByPrimaryKey(companyId);
  308. long logoId = company.getLogoId();
  309. if (logoId > 0) {
  310. company.setLogoId(0);
  311. companyPersistence.update(company, false);
  312. imageLocalService.deleteImage(logoId);
  313. }
  314. }
  315. public List<Company> getCompanies() throws SystemException {
  316. return companyPersistence.findAll();
  317. }
  318. public List<Company> getCompanies(boolean system) throws SystemException {
  319. return companyPersistence.findBySystem(system);
  320. }
  321. public int getCompaniesCount(boolean system) throws SystemException {
  322. return companyPersistence.countBySystem(system);
  323. }
  324. public Company getCompanyById(long companyId)
  325. throws PortalException, SystemException {
  326. return companyPersistence.findByPrimaryKey(companyId);
  327. }
  328. public Company getCompanyByLogoId(long logoId)
  329. throws PortalException, SystemException {
  330. return companyPersistence.findByLogoId(logoId);
  331. }
  332. public Company getCompanyByMx(String mx)
  333. throws PortalException, SystemException {
  334. return companyPersistence.findByMx(mx);
  335. }
  336. public Company getCompanyByVirtualHost(String virtualHostname)
  337. throws PortalException, SystemException {
  338. try {
  339. virtualHostname = virtualHostname.trim().toLowerCase();
  340. VirtualHost virtualHost = virtualHostPersistence.findByHostname(
  341. virtualHostname);
  342. if (virtualHost.getLayoutSetId() != 0) {
  343. throw new CompanyVirtualHostException(
  344. "Virtual host is associated with layout set " +
  345. virtualHost.getLayoutSetId());
  346. }
  347. return companyPersistence.findByPrimaryKey(
  348. virtualHost.getCompanyId());
  349. }
  350. catch (NoSuchVirtualHostException nsvhe) {
  351. throw new CompanyVirtualHostException(nsvhe);
  352. }
  353. }
  354. public Company getCompanyByWebId(String webId)
  355. throws PortalException, SystemException {
  356. return companyPersistence.findByWebId(webId);
  357. }
  358. public void removePreferences(long companyId, String[] keys)
  359. throws SystemException {
  360. PortletPreferences preferences = PrefsPropsUtil.getPreferences(
  361. companyId);
  362. try {
  363. for (String key : keys) {
  364. preferences.reset(key);
  365. }
  366. preferences.store();
  367. }
  368. catch (Exception e) {
  369. throw new SystemException(e);
  370. }
  371. }
  372. public Hits search(
  373. long companyId, long userId, String keywords, int start, int end)
  374. throws SystemException {
  375. return search(companyId, userId, null, 0, null, keywords, start, end);
  376. }
  377. public Hits search(
  378. long companyId, long userId, String portletId, long groupId,
  379. String type, String keywords, int start, int end)
  380. throws SystemException {
  381. try {
  382. BooleanQuery contextQuery = BooleanQueryFactoryUtil.create();
  383. contextQuery.addRequiredTerm(Field.COMPANY_ID, companyId);
  384. if (Validator.isNotNull(portletId)) {
  385. contextQuery.addRequiredTerm(Field.PORTLET_ID, portletId);
  386. }
  387. if (groupId > 0) {
  388. contextQuery.addRequiredTerm(Field.GROUP_ID, groupId);
  389. }
  390. if (Validator.isNotNull(type)) {
  391. contextQuery.addRequiredTerm(Field.TYPE, type);
  392. }
  393. BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
  394. searchQuery.addTerms(_KEYWORDS_FIELDS, keywords);
  395. BooleanQuery fullQuery = BooleanQueryFactoryUtil.create();
  396. fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
  397. if (searchQuery.clauses().size() > 0) {
  398. fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
  399. }
  400. return SearchEngineUtil.search(
  401. companyId, new long[] {groupId}, userId, null, fullQuery, start,
  402. end);
  403. }
  404. catch (Exception e) {
  405. throw new SystemException(e);
  406. }
  407. }
  408. public Company updateCompany(
  409. long companyId, String virtualHostname, String mx, int maxUsers)
  410. throws PortalException, SystemException {
  411. // Company
  412. virtualHostname = virtualHostname.trim().toLowerCase();
  413. Company company = companyPersistence.findByPrimaryKey(companyId);
  414. validate(company.getWebId(), virtualHostname, mx);
  415. if (PropsValues.MAIL_MX_UPDATE) {
  416. company.setMx(mx);
  417. }
  418. company.setMaxUsers(maxUsers);
  419. companyPersistence.update(company, false);
  420. // Virtual host
  421. updateVirtualHost(companyId, virtualHostname);
  422. return company;
  423. }
  424. public Company updateCompany(
  425. long companyId, String virtualHostname, String mx, String homeURL,
  426. String name, String legalName, String legalId, String legalType,
  427. String sicCode, String tickerSymbol, String industry, String type,
  428. String size)
  429. throws PortalException, SystemException {
  430. // Company
  431. virtualHostname = virtualHostname.trim().toLowerCase();
  432. Date now = new Date();
  433. Company company = companyPersistence.findByPrimaryKey(companyId);
  434. validate(company.getWebId(), virtualHostname, mx);
  435. validate(name);
  436. if (PropsValues.MAIL_MX_UPDATE) {
  437. company.setMx(mx);
  438. }
  439. company.setHomeURL(homeURL);
  440. companyPersistence.update(company, false);
  441. // Account
  442. Account account = accountPersistence.fetchByPrimaryKey(
  443. company.getAccountId());
  444. if (account == null) {
  445. long accountId = counterLocalService.increment();
  446. account = accountPersistence.create(accountId);
  447. account.setCreateDate(now);
  448. account.setCompanyId(companyId);
  449. account.setUserId(0);
  450. account.setUserName(StringPool.BLANK);
  451. company.setAccountId(accountId);
  452. companyPersistence.update(company, false);
  453. }
  454. account.setModifiedDate(now);
  455. account.setName(name);
  456. account.setLegalName(legalName);
  457. account.setLegalId(legalId);
  458. account.setLegalType(legalType);
  459. account.setSicCode(sicCode);
  460. account.setTickerSymbol(tickerSymbol);
  461. account.setIndustry(industry);
  462. account.setType(type);
  463. account.setSize(size);
  464. accountPersistence.update(account, false);
  465. // Virtual host
  466. updateVirtualHost(companyId, virtualHostname);
  467. return company;
  468. }
  469. public void updateDisplay(
  470. long companyId, String languageId, String timeZoneId)
  471. throws PortalException, SystemException {
  472. User user = userLocalService.getDefaultUser(companyId);
  473. user.setLanguageId(languageId);
  474. user.setTimeZoneId(timeZoneId);
  475. userPersistence.update(user, false);
  476. }
  477. public void updateLogo(long companyId, byte[] bytes)
  478. throws PortalException, SystemException {
  479. long logoId = getLogoId(companyId);
  480. imageLocalService.updateImage(logoId, bytes);
  481. }
  482. public void updateLogo(long companyId, File file)
  483. throws PortalException, SystemException {
  484. long logoId = getLogoId(companyId);
  485. imageLocalService.updateImage(logoId, file);
  486. }
  487. public void updateLogo(long companyId, InputStream is)
  488. throws PortalException, SystemException {
  489. long logoId = getLogoId(companyId);
  490. imageLocalService.updateImage(logoId, is);
  491. }
  492. public void updatePreferences(long companyId, UnicodeProperties properties)
  493. throws SystemException {
  494. PortletPreferences preferences = PrefsPropsUtil.getPreferences(
  495. companyId);
  496. try {
  497. if (properties.containsKey(PropsKeys.LOCALES)) {
  498. String oldLocales = preferences.getValue(
  499. PropsKeys.LOCALES, StringPool.BLANK);
  500. String newLocales = properties.getProperty(PropsKeys.LOCALES);
  501. if (!Validator.equals(oldLocales, newLocales)) {
  502. LanguageUtil.resetAvailableLocales(companyId);
  503. }
  504. }
  505. List<String> resetKeys = new ArrayList<String>();
  506. for (Map.Entry<String, String> entry : properties.entrySet()) {
  507. String key = entry.getKey();
  508. String value = entry.getValue();
  509. if (value.equals(Portal.TEMP_OBFUSCATION_VALUE)) {
  510. continue;
  511. }
  512. String propsUtilValue = PropsUtil.get(key);
  513. if (!value.equals(propsUtilValue)) {
  514. preferences.setValue(key, value);
  515. }
  516. else {
  517. String preferencesValue = preferences.getValue(key, null);
  518. if (preferencesValue != null) {
  519. resetKeys.add(key);
  520. }
  521. }
  522. }
  523. preferences.store();
  524. if (!resetKeys.isEmpty()) {
  525. removePreferences(
  526. companyId, resetKeys.toArray(new String[resetKeys.size()]));
  527. }
  528. }
  529. catch (Exception e) {
  530. throw new SystemException(e);
  531. }
  532. }
  533. public void updateSecurity(
  534. long companyId, String authType, boolean autoLogin,
  535. boolean sendPassword, boolean strangers, boolean strangersWithMx,
  536. boolean strangersVerify, boolean communityLogo)
  537. throws SystemException {
  538. PortletPreferences preferences = PrefsPropsUtil.getPreferences(
  539. companyId);
  540. try {
  541. preferences.setValue(
  542. PropsKeys.COMPANY_SECURITY_AUTH_TYPE, authType);
  543. preferences.setValue(
  544. PropsKeys.COMPANY_SECURITY_AUTO_LOGIN,
  545. String.valueOf(autoLogin));
  546. preferences.setValue(
  547. PropsKeys.COMPANY_SECURITY_SEND_PASSWORD,
  548. String.valueOf(sendPassword));
  549. preferences.setValue(
  550. PropsKeys.COMPANY_SECURITY_STRANGERS,
  551. String.valueOf(strangers));
  552. preferences.setValue(
  553. PropsKeys.COMPANY_SECURITY_STRANGERS_WITH_MX,
  554. String.valueOf(strangersWithMx));
  555. preferences.setValue(
  556. PropsKeys.COMPANY_SECURITY_STRANGERS_VERIFY,
  557. String.valueOf(strangersVerify));
  558. preferences.setValue(
  559. PropsKeys.COMPANY_SECURITY_COMMUNITY_LOGO,
  560. String.valueOf(communityLogo));
  561. preferences.store();
  562. }
  563. catch (IOException ioe) {
  564. throw new SystemException(ioe);
  565. }
  566. catch (PortletException pe) {
  567. throw new SystemException(pe);
  568. }
  569. }
  570. protected long getLogoId(long companyId)
  571. throws PortalException, SystemException {
  572. Company company = companyPersistence.findByPrimaryKey(companyId);
  573. long logoId = company.getLogoId();
  574. if (logoId <= 0) {
  575. logoId = counterLocalService.increment();
  576. company.setLogoId(logoId);
  577. companyPersistence.update(company, false);
  578. }
  579. return logoId;
  580. }
  581. protected void updateVirtualHost(long companyId, String virtualHostname)
  582. throws CompanyVirtualHostException, SystemException {
  583. if (Validator.isNotNull(virtualHostname)) {
  584. try {
  585. VirtualHost virtualHost = virtualHostPersistence.findByHostname(
  586. virtualHostname);
  587. if ((virtualHost.getCompanyId() != companyId) ||
  588. (virtualHost.getLayoutSetId() != 0)) {
  589. throw new CompanyVirtualHostException();
  590. }
  591. }
  592. catch (NoSuchVirtualHostException nsvhe) {
  593. virtualHostLocalService.updateVirtualHost(
  594. companyId, 0, virtualHostname);
  595. }
  596. }
  597. else {
  598. try {
  599. virtualHostPersistence.removeByC_L(companyId, 0);
  600. }
  601. catch (NoSuchVirtualHostException nsvhe) {
  602. }
  603. }
  604. }
  605. protected void validate(String name) throws PortalException {
  606. if (Validator.isNull(name)) {
  607. throw new AccountNameException();
  608. }
  609. }
  610. protected void validate(String webId, String virtualHostname, String mx)
  611. throws PortalException, SystemException {
  612. if (Validator.isNull(virtualHostname)) {
  613. throw new CompanyVirtualHostException();
  614. }
  615. else if (virtualHostname.equals(_DEFAULT_VIRTUAL_HOST) &&
  616. !webId.equals(PropsValues.COMPANY_DEFAULT_WEB_ID)) {
  617. throw new CompanyVirtualHostException();
  618. }
  619. else if (!Validator.isDomain(virtualHostname)) {
  620. throw new CompanyVirtualHostException();
  621. }
  622. else {
  623. try {
  624. VirtualHost virtualHost = virtualHostPersistence.findByHostname(
  625. virtualHostname);
  626. long companyId = virtualHost.getCompanyId();
  627. Company virtualHostnameCompany =
  628. companyPersistence.findByPrimaryKey(companyId);
  629. if (!virtualHostnameCompany.getWebId().equals(webId)) {
  630. throw new CompanyVirtualHostException();
  631. }
  632. }
  633. catch (NoSuchVirtualHostException nsvhe) {
  634. }
  635. }
  636. if (Validator.isNull(mx)) {
  637. throw new CompanyMxException();
  638. }
  639. else if (!Validator.isDomain(mx)) {
  640. throw new CompanyMxException();
  641. }
  642. }
  643. private static final String _DEFAULT_VIRTUAL_HOST = "localhost";
  644. private static final String[] _KEYWORDS_FIELDS = {
  645. Field.ASSET_TAG_NAMES, Field.CONTENT, Field.DESCRIPTION,
  646. Field.PROPERTIES, Field.TITLE
  647. };
  648. }